【发布时间】:2011-12-12 14:41:47
【问题描述】:
我正在基类中实现 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.
}
-snip-
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
}
}
-snip-
我正在使用此 DummyPerson 及其 Name 属性将数据绑定到 UI。此后的第二次和所有后续尝试都会导致NullReferenceException。
【问题讨论】:
-
属性
Name是否存在? -
这个问题有解决方案吗?我现在遇到了这个问题。
-
不,我没有找到它,虽然我确定这是 IIS 中的一些问题 - 我最终从头开始重新安装整个操作系统:|
标签: c# .net mvvm nullreferenceexception inotifypropertychanged