使用INotifyPropertyChanged实现的奇怪的NullReferenceException

rum*_*fx0 6 .net c# mvvm inotifypropertychanged nullreferenceexception

我正在基类中实现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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用它如下:

RaisePropertyChanged("Name");
Run Code Online (Sandbox Code Playgroud)

我得到一个NullReferenceException,而参数,"this"和处理程序是非null.任何人都可以对此有所了解吗?

谢谢.

- >例外的完整堆栈跟踪:http://pastebin.com/bH9FeurJ

更新当我覆盖包含此属性的类的实例时发生异常.简化示例:

public class Person : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }

// More properties etc.
}
Run Code Online (Sandbox Code Playgroud)

-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
    }
}
Run Code Online (Sandbox Code Playgroud)

-snip-

我正在使用它DummyPerson和它的Name属性来数据绑定到UI.之后的第二次和所有后续尝试都会导致NullReferenceException.

Dav*_*rts 1

我已经遇到这个错误有一段时间了,但是,我现在已经解决了它(尽管这可能是我的代码中的不同原因)-我(相当愚蠢地)没有在我的 IValueConverter 实现之一中检查 null ,(并且对于由于某些原因,代码不允许我进入此代码)并导致异常,因为 null 作为值传入。