Windows phone 7 无法使用MVVM将textblock属性从另一个类绑定到UI类。(第2卷)

Windows phone 7 无法使用MVVM将textblock属性从另一个类绑定到UI类。(第2卷),windows-phone-7,Windows Phone 7,在上一篇文章中,我问过不能使用MVVM将textblock属性从另一个类绑定到UI类。 我仍然无法绑定textblock属性,但我发现了一个新情况,即当我无法绑定textblock属性时,PropertyChanged事件变为null 请参见下面的代码(也可参见上一篇文章): 在下面的代码中,PropertyChanged变为null public class ViewModelBase : INotifyPropertyChanged { public event PropertyC

在上一篇文章中,我问过不能使用MVVM将textblock属性从另一个类绑定到UI类。

我仍然无法绑定textblock属性,但我发现了一个新情况,即当我无法绑定textblock属性时,PropertyChanged事件变为null

请参见下面的代码(也可参见上一篇文章):

在下面的代码中,PropertyChanged变为null

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        //PropertyChanged is null, so event is not called and ErrorStatus is not changed.
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
请让我知道如何编写正确的代码以及PropertyChanged变为null的原因

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        //PropertyChanged is null, so event is not called and ErrorStatus is not changed.
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

我已经确认,当在UI类(MainPage.cs)中调用ErrorStatus时,ErrorStatus会正确更改。

我不知道这是否解决了您的问题,但是:

触发事件的方式不是线程安全的。不仅在这里,而且总是像这样激发您的活动:

protected void NotifyPropertyChanged(String info)
{
    var handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(info));
    }
}