2012-12-18 22 views
5

Trong MvvmLight, tôi thấy rằng bên cạnh Messenger.Default là một loại biến tĩnh toàn cầu để lưu trữ xử lý tin nhắn của toàn bộ ứng dụng, mỗi ViewModel sẽ có một Trình xử lý Messenger khác có tên là MessengerInstance. Vì vậy, tôi đang nhầm lẫn về MessengerInstance được sử dụng cho & làm thế nào để sử dụng nó? (Chỉ ViewModel có thể nhìn thấy nó -> ai sẽ nhận & nhắn quá trình?)MessengerInstance vs Messenger.Default trong Mvvmlight

Trả lời

3

Các MessengerInstance được sử dụng bởi RaisePropertyChanged() phương pháp:

<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ạn có thể sử dụng nó trên một tài sản trên một cái nhìn mô hình B ví dụ:

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

Catch nó và đối phó với nó trên một mô hình điểm A với:

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

Hy vọng điều này sẽ giúp :)

+0

Cảm ơn! Xóa ngay!^_ ^ – kidgu

Các vấn đề liên quan