2016-05-04 78 views
0

我在C#8.1的Windows Store應用MVVM模式。通知父視圖模型在MVVM

我有3周的ViewModels: 1. BaseViewModel 2. StudentViewModel 3. StudentDashboardViewModel

它像:

  • 我加入BaseViewModel
  • 我繼承了BaseViewModel的StudentViewModel
  • 然後,我從StudentViewModel繼承了StudentDashboardViewModel

我做了一個頁面StudentDashboardPage,我又把它綁定到StudentDashboardViewModel

我試圖更改屬性例如IsBlackInStudentViewModel通過另一個類,但問題是,它不通知其子視圖模型StudentDashboardViewModel

那麼,如何通知child viewmodel父視圖模型的變化。 下面是代碼:

BaseViewModel:

public class BaseViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null) 
     { 
      if (Object.Equals(storage, value)) 
      { 
       return false; 
      } 
      storage = value; 
      this.OnPropertyChanged(propertyName); 
      return true; 
     } 
     protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      var eventHandler = this.PropertyChanged; 
      if (eventHandler != null) 
      { 
       eventHandler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     }    
    } 

StudentViewModel:

public class StudentViewModel : BaseViewModel 
    {  
     private static StudentViewModel studentViewModel;    
     public static StudentViewModel Singleton() 
     { 
      if (studentViewModel == null) 
      { 
       studentViewModel = new StudentViewModel(); 
      } 
      return studentViewModel; 
     } 

     private bool _IsBlackIn = false; 
     public bool IsBlackIn 
     { 
      get { return _IsBlackIn; } 
      set 
      { 
       SetProperty<bool>(ref _IsBlackIn, value);    
      } 
     } 
    } 

StudentDashboardViewModel:

public class StudentDashboardViewModel : StudentViewModel 
{ 
    public static StudentDashboardViewModel studentDashboardViewModel; 

    public static StudentDashboardViewModel GetSingleInstance() 
    { 
     return studentDashboardViewModel ?? (studentDashboardViewModel = new StudentDashboardViewModel()); 
    } 

} 
後面的代碼0

StudentDashboardPage頁:

public sealed partial class StudentDashboardPage : Page 
{ 
    private StudentDashboardViewModel studentDashvm; 

    public StudentDashboardPage() 
    { 
     this.InitializeComponent();   
     this.Loaded += StudentDashboardPage_Loaded; 
    } 

    private void StudentDashboardPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     this.studentDashvm = StudentDashboardViewModel.GetSingleInstance(); 
     this.DataContext = studentDashvm; 
    } 
} 
+3

返回到MVVM基礎知識。爲什麼你的ViewModels單身居首位?只有在顯示相應的視圖時,ViewModel纔會處於活動狀態,而不是整個應用程序的使用壽命。 – niksofteng

+0

我打算從單身人士恢復,但通知問題呢? –

+0

視圖模型可以超越他們的觀點,甚至可以被多個視圖共享,但我同意他們不應該是單身人士。 –

回答

2

有3種方式,我可以看到StudentDashboardViewModel可以得到通知,可以發生在StudentViewModel定義的IsBlackIn屬性的任何變化:

  1. 製作SetProperty<T>(在BaseViewModel中)虛擬並覆蓋StudentDashboardViewModel並檢查屬性的名稱。
  2. 使IsBlackIn財產的執行者StudentViewModel爲虛擬並在StudentDashboardViewModel中覆蓋。

在前兩種情況下,不要忘記調用基本方法,無論是SetProperty<T>還是IsBlackIn設置方法。

您擁有的第三個選項是組合視圖模型(使用繼承的組合)。也就是說,讓StudentDashboardViewModel接收StudentViewModel的實例,然後監聽通知IsBlackIn更改的事件。您可以聽取INotifyPropertyChanged.PropertyChanged或實施您自己的自定義事件。

+0

選項3,選項3!爲了所有聖潔的愛! – Jamiec