2012-12-18 14 views
5

MvvmLightでは、アプリケーション全体のメッセージハンドルを格納する一種のグローバル静的変数であるMessenger.Defaultの横に、それぞれのViewModelにMessengerInstanceという名前の別のMessenger Handlerがあることがわかりました。 それで、私はMessengerInstanceが使用されていることについて混乱しています&どのようにそれを使用するには? (のみのViewModelがそれを見ることができます - > &プロセスメッセージが表示されます誰が?)MessengerInstanceとMessenger.Default in Mvvmlight

答えて

3

MessengerInstanceがRaisePropertyChanged()メソッドで使用されている:あなたはビューのプロパティにそれを使用することができ

<summary> 
/// Raises the PropertyChanged event if needed, and broadcasts a 
///    PropertyChangedMessage using the Messenger instance (or the 
///    static default instance if no Messenger instance is available). 
/// 
/// </summary> 
/// <typeparam name="T">The type of the property that 
///    changed.</typeparam> 
/// <param name="propertyName">The name of the property 
///    that changed.</param> 
/// <param name="oldValue">The property's value before the change 
///    occurred.</param> 
/// <param name="newValue">The property's value after the change 
///    occurred.</param> 
/// <param name="broadcast">If true, a PropertyChangedMessage will 
///    be broadcasted. If false, only the event will be raised.</param> 
protected virtual void RaisePropertyChanged<T>(string propertyName, T oldValue, T  
               newValue, bool broadcast); 

例えば、モデルB:

public const string SelectedCommuneName = "SelectedCommune"; 

    private communes selectedCommune; 

    public communes SelectedCommune 
    { 
     get { return selectedCommune; } 

     set 
     { 
      if (selectedCommune == value) 
       return; 

      var oldValue = selectedCommune; 
      selectedCommune = value; 

      RaisePropertyChanged(SelectedCommuneName, oldValue, value, true); 
     } 
    } 

キャッチそれとでビューモデルAにそれに対処:

Messenger.Default.Register<PropertyChangedMessage<communes>>(this, (nouvelleCommune) => 
     { 
      //Actions to perform 
      Client.Ville = nouvelleCommune.NewValue.libelle; 
      Client.CodePays = nouvelleCommune.NewValue.code_pays; 
      Client.CodePostal = nouvelleCommune.NewValue.code_postal; 
     }); 

希望すると便利です:)

+0

ありがとうございます!今すぐクリア!^_ ^ – kidgu

関連する問題