2011-07-17 2 views
0

前の投稿のMVVMを使用して、別のクラスのテキストブロックプロパティをUIクラスにバインドできませんでした。 Can not bind textblock property from another class to UI class using MVVMMVVMを使用して、別のクラスからUIクラスにtextblockプロパティをバインドできません。(vol。2)

私はまだtextblockプロパティをバインドすることはできませんが、textblockプロパティをバインドできない場合、PropertyChangedイベントがnullになるという新しいことが見つかりました。

(また以前の記事を参照してください)怒鳴るコードを参照してください。

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が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になるのか教えてください。

ErrorStatusがUIクラス(MainPage.cs)で呼び出されたときに、ErrorStatusが正しく変更されることを既に確認しました。

答えて

0

私はこれがあなたの問題を解決していないかどうかを知る、しかしません:

あなたはスレッドセーフではありませんイベントを発射している方法。ここだけでなく、常にこのようなイベントを発射してください:

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

ありがとうございます。しかし、それはあなたのソリューションを使用して動作しませんでした。 – okame100

関連する問題