2012-12-18 13 views
5

In MvvmLight, ho trovato che accanto a Messenger.Default che è una specie di variabile statica globale per memorizzare l'handle di messaggio dell'intera applicazione, ogni Viewmodel avrà un altro gestore di Messenger denominato MessengerInstance. Quindi, sono confuso su cosa sia MessengerInstance utilizzato per & come usarlo? (Solo ViewModel può vedere -> che riceverà & messaggio processo?)MessengerInstance vs Messenger.Default in Mvvmlight

risposta

3

Il MessengerInstance viene utilizzato dal RaisePropertyChanged() Metodo:

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

si può utilizzare su una proprietà su una vista modello B per esempio:

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

Cattura e trattare con esso su un modello di vista a con:

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

Spero che questo aiuti :)

+0

Grazie! Chiaro ora!^_ ^ – kidgu

Problemi correlati