public class ViewModel : INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
}
protected void OnPropertyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
public PropertyChangedEventHandler PropertyChanged;
}
public partial class MainWindow : Window
{
private ViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new ViewModel { Name = "Shahrukh Khan" };
DataContext = vm;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
vm.Name = "Salman Khan";
}
}
当我运行以下代码时,我收到此错误:
INotifyUnderstandingYoutube.ViewModel
未实现接口成员System.ComponentModel.INotifyPropertyChanged.PropertyChanged
我不明白这背后的原因?任何人都可以解释一下这是什么错误吗?
答案 0 :(得分:5)
您
public PropertyChangedEventHandler PropertyChanged;
将PropertyChanged
定义为一个字段,它恰好是委托类型的字段,但仍然是一个字段。 INotifyPropertyChanged
预示着一个事件。
public event PropertyChangedEventHandler PropertyChanged;
^^^^^
答案 1 :(得分:0)
这意味着您没有完全实现接口。在接口声明(在:INotifyPropertyChanged之后),按Shift + Alt + F10,让Visual Studio为您添加代码。然后,您将看到您遗失的内容。
好像你在ViewModel类中缺少这段代码:
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add { throw new NotImplementedException(); }
remove { throw new NotImplementedException(); }
}