그래서 ViewModel의 속성을 TextBox의 속성에 바인딩했으며 INotifyPropertyChanged 구현을 사용하여 알맞은 위치에 알림을 표시합니다. 그러나 TextBox에서 데이터가 업데이트되지 않습니다. 내 오류는 어디에 있습니까?C# 6.0에서 INotifyPropertyChanged 구현 - 왜 결과가 없습니까?
// ViewModel
namespace App1
{
public class Result
{
public string Message { get; set; }
}
public class ResultViewModel : INotifyPropertyChanged
{
private StringBuilder sb = new StringBuilder();
private CoreDispatcher dispatcher;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public string Results
{
get { return sb.ToString(); }
}
public int Count()
{
return sb.Length;
}
public ResultViewModel()
{
dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
}
public async Task Clear()
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => sb.Clear());
}
public async Task Add(Result result)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => sb.Append(result.Message));
OnPropertyChanged(nameof(Results));
}
}
}
추가 다음과 같이 MainPage.xaml.cs를에 함수에 의해 호출됩니다 ... 텍스트 상자은 다음과 같습니다에 관해서는
private async void ShowResult(Result result)
{
await this.ViewModel.Add(result);
}
...
// XAML
<TextBox x:Name="content"
Margin="0,10,0,10"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.Below="header"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.Above="genButton"
TextWrapping="Wrap"
Text="{Binding Path=ViewModel.Results, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextChanged="content_TextChanged"/>
바인딩은 'OneWay'모드를 사용합니다. 바인딩이가는 "방법"에 대해 확신합니까? – Abion47
누가'Add()'메서드를 호출하고 있습니까? '[CallerMemberName]'대신'OnPropertyChanged ("Results");를 호출해야합니다. 호출자가 속성이지만 올바른 (속성) 이름이 전달되지 않았다고 가정하고 있습니다. –
정확한 정보가 Invoke에 전달되고 UpdateSourceTrigger가 PropertyChanged를 가리키고 Mode가 TwoWay가되었지만 아직 TextBox에서 데이터를 가져 오지 못했습니다. – dspfnder