<分区>
我在基类中实现 INotifyPropertyChanged,如下所示:
public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
var propChangedHandler = PropertyChanged;
if (propChangedHandler != null)
{
var args = new PropertyChangedEventArgs(propertyName);
propChangedHandler(this, args);
}
}
}
我是这样使用它的:
RaisePropertyChanged("Name");
当参数“this”和处理程序不为空时,我收到 NullReferenceException。任何人都可以阐明这一点吗?
谢谢。
-> 异常的完整堆栈跟踪:http://pastebin.com/bH9FeurJ
UPDATE 当我覆盖包含此属性的类的实例时发生异常。简化示例:
public class Person : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
// More properties etc.
}
-剪辑-
public class ViewModel
{
private Person _dummyPerson;
public Person DummyPerson
{
get { return _dummyPerson; }
set
{
_dummyPerson = value;
RaisePropertyChanged("DummyPerson");
}
}
public void Foo()
{
DummyPerson = new DummyPerson();
// this line throws the NRE, strangly enough the very FIRST time it works fine
}
}
-剪辑-
我正在使用此 DummyPerson
及其 Name
属性将数据绑定(bind)到 UI。此后的第二次和所有后续尝试都会导致 NullReferenceException
。