2011-07-17 14 views
0

我問在上一篇文章中,不能將textblock屬性從另一個類綁定到使用MVVM的UI類。 Can not bind textblock property from another class to UI class using MVVM無法將textblock屬性從另一個類綁定到使用MVVM的UI類。(第2卷)

我仍然無法綁定textblock屬性,但我發現了一個新的事物,即當我無法綁定textblock屬性時,PropertyChanged事件變爲空。

請參見下文碼(見以前的帖子):

public class Authentication : ViewModelBase 
{ 
    private string _ErrorStatus; 
    public string ErrorStatus 
    { 
     get 
     { 
      return _ErrorStatus; 
     } 
     set 
     { 
      _ErrorStatus = value; 
      NotifyPropertyChanged("ErrorStatus"); 
     } 
    } 

    void Authenticate() 
    { 
     //The bellow code doesn't work. 
     ErrorStatus = "Access Denied."; 
    } 
} 

在婁代碼,成爲的PropertyChanged空。

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。

我已經確認ErrorStatus在UI類(MainPage.cs)中調用ErrorStatus時正確更改。

回答

0

我不知道這樣做是否解決您的問題或沒有,但:

你是射擊事件是不是線程安全的方式。不僅在這裏,但總是這樣激發你的事件:

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

謝謝答覆。但它沒有用你的解決方案。 – okame100

相關問題