C# PropertyChanged仅在第一个事件上为null
我正在准备用WPF编写代码。所以我想出了一些基本的模拟代码来让自己开始。我有一个简单的WPF界面,其中有一个日期选择器、一些文本框和复选框 我想对INotifyPropertyChanged界面做的是,当我单击按钮时,我的文本框将显示“Hello Kevin”。为了测试它,我首先说我的字符串将以“Initially”开头,当我的方法返回时,它将返回“Hello Kevin” 我还应该指定我编写了一个模拟异步代码,该代码将调用服务器并在数据库中获取名称。这段代码运行得很好。问题是,当我第一次点击按钮时,PropretyChanged是空的。但是,当我再次单击它时,它会显示我想看到的消息 它让我想知道这个方法是否真的像我预期的那样工作,所以我用Console.WriteLine方法做了一些基本的测试,它只会显示我目前正在寻找的结果 下面,您可以找到我用于这个小软件的所有类:C# PropertyChanged仅在第一个事件上为null,c#,wpf,asynchronous,C#,Wpf,Asynchronous,我正在准备用WPF编写代码。所以我想出了一些基本的模拟代码来让自己开始。我有一个简单的WPF界面,其中有一个日期选择器、一些文本框和复选框 我想对INotifyPropertyChanged界面做的是,当我单击按钮时,我的文本框将显示“Hello Kevin”。为了测试它,我首先说我的字符串将以“Initially”开头,当我的方法返回时,它将返回“Hello Kevin” 我还应该指定我编写了一个模拟异步代码,该代码将调用服务器并在数据库中获取名称。这段代码运行得很好。问题是,当我第一次点击按
public class ViewModelCommonBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public class SimpleViewModel : ViewModelCommonBase
{
#region fields
private bool _isHealthCareEnabled=false;
private bool _isBenefits=false;
private bool _is401kEnabled=false;
private DateTime _birth;
#endregion
#region Properties
public int Age
{
get { return (int)((DateTime.Now - Birth).TotalDays)/365 ;}
}
public bool IsHealthCareEnabled
{
get { return _isHealthCareEnabled; }
set { _isHealthCareEnabled = value; }
}
public bool IsBenefitsEnabled
{
get { return _isBenefits; }
set
{
if (_isBenefits != value)
{
_isBenefits = value;
OnPropertyChanged("IsBenefitsEnabled");
}
}
}
public bool Is401kEnabled
{
get { return _is401kEnabled; }
set { _is401kEnabled = value; }
}
public DateTime Birth
{
get { return _birth; }
set
{
if (_birth != value)
{
_birth = value;
OnPropertyChanged("Birth");
OnPropertyChanged("Age");
}
}
}
#endregion
}
public class AsyncServerStud :ViewModelCommonBase
{
private string _nameOfPerson="Initial";
public string NameOfPerson
{
get { return _nameOfPerson; }
set
{
if (_nameOfPerson != value)
{
_nameOfPerson = value;
OnPropertyChanged("NameOfPerson");
}
}
}
public async void FindNameOfPerson()
{
var result = await FindNameOfPersonAsync("Kevin");
NameOfPerson = result;
Console.WriteLine(NameOfPerson);
}
private Task<string> FindNameOfPersonAsync(string name)
{
return Task.Factory.StartNew(() => SearchNameOfPerson(name));
}
private string SearchNameOfPerson(string name)
{
Thread.Sleep(6000);
return "Hello, " + name;
}
}
public class WPFViewModelPage
{
public SimpleViewModel SimpleViewModel { get; set; }
public AsyncServerStud MockServer { get; set; }
}
我对你的申请做了以下更改,它对我有效。请尝试以下更改 将
DataContext
分配到Window
而不是将其设置为Grid
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApplication1"
Title="MainWindow"
Height="350"
Width="525">
<Window.DataContext>
<vm:WPFViewModelPage />
</Window.DataContext>
<Window.Resources>
<vm:WPFViewModelPage x:Key="viewModel" />
</Window.Resources>
<Grid Name="myGrid">
<CheckBox x:Name="chkBenefits"
Content="Has Benefits ?"
HorizontalAlignment="Left"
Margin="62,45,0,0"
VerticalAlignment="Top"
IsChecked="{Binding SimpleViewModel.IsBenefitsEnabled}" />
<StackPanel IsEnabled="{Binding SimpleViewModel.IsBenefitsEnabled}">
<CheckBox Content="Health care"
HorizontalAlignment="Left"
Margin="85,86,0,0"
VerticalAlignment="Top"
IsEnabled="{Binding SimpleViewModel.IsBenefitsEnabled}"
IsChecked="{Binding SimpleViewModel.IsHealthCareEnabled}" />
<DatePicker Name="mydatePicker"
DisplayDate="{Binding SimpleViewModel.Birth}" />
<TextBlock TextWrapping="Wrap"
Text="{Binding SimpleViewModel.Age}" />
<Button Content="Button"
Click="Button_Click" />
<TextBox Name="mySecondTextBox"
Height="23"
TextWrapping="Wrap"
Text="{Binding MockServer.NameOfPerson }" />
</StackPanel>
</Grid>
</Window>
在按钮中添加以下代码\u单击方法
private void Button_Click(object sender, RoutedEventArgs e)
{
WPFViewModelPage dataContext = this.DataContext as WPFViewModelPage;
dataContext.MockServer.FindNameOfPerson();
}
PropertyChanged
总是空的,这似乎很奇怪。好的,只要您设置了DataContext
,它就会被分配到。你是用一种奇怪的方式设置的吗?加上它,你就不会点击按钮了吗?如果你想要一个异步get,为什么不直接使用异步get呢。甚至还有一个优先级绑定。是的,谢谢,这是我在编辑之前更改的。别误会我的意思,我非常感谢你花时间给我一个答案。当我在XAML中进行数据绑定时,我想知道为什么数据绑定不能正常工作。实际上,不管我说了什么,我没有将数据上下文放在click事件中,为什么要这样做?我只在XAML中设置DataContext,并从Button_click访问相同的DataContext。我在回答中更新了整个XAML。请检查为什么XAML不工作,因为您正在将DataContext设置为网格,如DataContext=“{Binding Source={StaticResource viewModel}}”,所以当然窗口DataContext将为空。您可以通过访问按钮中的myGrid.DataContext来解决此问题
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApplication1"
Title="MainWindow"
Height="350"
Width="525">
<Window.DataContext>
<vm:WPFViewModelPage />
</Window.DataContext>
<Window.Resources>
<vm:WPFViewModelPage x:Key="viewModel" />
</Window.Resources>
<Grid Name="myGrid">
<CheckBox x:Name="chkBenefits"
Content="Has Benefits ?"
HorizontalAlignment="Left"
Margin="62,45,0,0"
VerticalAlignment="Top"
IsChecked="{Binding SimpleViewModel.IsBenefitsEnabled}" />
<StackPanel IsEnabled="{Binding SimpleViewModel.IsBenefitsEnabled}">
<CheckBox Content="Health care"
HorizontalAlignment="Left"
Margin="85,86,0,0"
VerticalAlignment="Top"
IsEnabled="{Binding SimpleViewModel.IsBenefitsEnabled}"
IsChecked="{Binding SimpleViewModel.IsHealthCareEnabled}" />
<DatePicker Name="mydatePicker"
DisplayDate="{Binding SimpleViewModel.Birth}" />
<TextBlock TextWrapping="Wrap"
Text="{Binding SimpleViewModel.Age}" />
<Button Content="Button"
Click="Button_Click" />
<TextBox Name="mySecondTextBox"
Height="23"
TextWrapping="Wrap"
Text="{Binding MockServer.NameOfPerson }" />
</StackPanel>
</Grid>
</Window>
public class WPFViewModelPage
{
public SimpleViewModel SimpleViewModel { get; set; }
public AsyncServerStud MockServer { get; set; }
public WPFViewModelPage()
{
this.SimpleViewModel = new SimpleViewModel();
this.MockServer = new AsyncServerStud();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
WPFViewModelPage dataContext = this.DataContext as WPFViewModelPage;
dataContext.MockServer.FindNameOfPerson();
}