2012-12-18 11 views
5

In MvvmLight fand ich, dass neben Messenger.Default, die eine Art globale statische Variable ist, um das Nachrichtenhandle der gesamten Anwendung zu speichern, jedes Viewmodel einen anderen Messenger Handler namens MessengerInstance haben wird. Also, ich bin verwirrt darüber, was ist MessengerInstance für & verwendet, wie man es benutzt? (Nur Ansichtsmodell kann es sehen -> die & Prozessmeldung erhalten?)MessengerInstance vs Messenger.Default in Mvvmlight

Antwort

3

Die MessengerInstance vom RaisePropertyChanged verwendet wird() -Methode:

<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); 

Sie können es auf einem Grundstück auf einer Ansicht verwenden Modell B zum Beispiel:

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); 
     } 
    } 

fangen sie und mit ihr auf einen Blick Modell A beschäftigen sich mit:

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; 
     }); 

Hoffe, das wird helfen :)

+0

Vielen Dank! Frei Jetzt!^_ ^ – kidgu