2016-04-11 16 views
1

我正在進入MVVM,並開始與Prism框架和我的示例應用程序中我有典型的導航。其中一頁應列出一些應用程序,我想知道實施中的差異。爲了簡單起見,這裏是來自代碼中的一些小片段迄今:ObservableCollection事件傳播和MVVM中的正確用法

應用模式

public class Application : BindableBase 
{ 
    private string _commandLine; 
    private string _name; 
    private string _smallIconSource; 

    public string CommandLine 
    { 
     get { return _commandLine; } 
     set { SetProperty(ref _commandLine, value); } 
    } 

    public string Name 
    { 
     get { return _name; } 
     set { SetProperty(ref _name, value); } 
    } 

    public string SmallIconSource 
    { 
     get { return _smallIconSource; } 
     set { SetProperty(ref _smallIconSource, value); } 
    } 
} 

ApplicationPage視圖模型

public class ApplicationPageViewModel : BindableBase 
{ 
    private ObservableCollection<Application> _applicationCollection; 

    public ApplicationPageViewModel() 
    { 
     // load some collection entries here 
    } 

    public ObservableCollection<Application> ApplicationCollection 
    { 
     get { return _applicationCollection; } 
     set 
     { 
      // if (_applicationCollection != null) 
      //  _applicationCollection.CollectionChanged -= ApplicationCollectionChanged; 
      SetProperty(ref _applicationCollection, value); 
      // if (_applicationCollection != null) 
      //  _applicationCollection.CollectionChanged += ApplicationCollectionChanged; 
     } 
    } 
} 

ApplicationPage查看

<!-- ... --> 
<ItemsControl ItemsSource="{Binding ApplicationCollection}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <!-- some kind of representation of applications --> 
      <Label Content="{Binding Name}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
<!-- ... --> 

在互聯網上的一些代碼示例中,特別是在SO上的一些問題中,我看到有人問是否存儲ViewModels的ObservableCollection,而不是像我一樣存儲模型 - 我很好奇在這個時候你會選擇那些版本在另一個版本上?

而且我想知道在Application類變化是否在ApplicationPageViewModel類反映,或者如果我有掛接到CollectionChanged事件(如布賴恩拉古納斯網絡研討會在哪裏見過我看到了這個技術)。

public event EventHandler CanExecuteChanged 
{ 
    add { CommandManager.RequerySuggested += value; } 
    remove { CommandManager.RequerySuggested -= value; } 
} 
+0

您的問題基本上是如何通知有關該集合在哪些項目被更改的項目。我可以想到:1)將ViewModel實例傳遞給每個項目(構造函數或其他),項目可以訂閱ViewModel的某些事件,事件由ViewModel提升(甚至通過項目)2)ViewModel處理所有更改和通知每個項目方法在收集變化)。如果你實現了'INotifyPropertyChanged'(這是你正在收聽的事件),那麼方法(1)就會出現在框中。 – Sinatr

回答

0

我想,如果在屬性中使用的ViewModels:到目前爲止,我只看到了這個鉤入CollectionChanged事件調用RaiseCanExecuteChanged方法,如果DelegateCommands手動防止unnecesary膨脹具有以下實現的RelayCommand■當/調用您的收藏更改。如果一個項目上的數據沒有改變,那麼如果你使用虛擬機或者模型,那麼它並沒有真正的區別,只是你更喜歡它。 我個人喜歡用ViewModels封裝我的模型,因爲我可以很容易地添加組合的屬性來顯示,我不想直接在模型中添加。

如果你想要做的ApplicationPageViewModel一些動作時,應用程序的變化,你必須

  • 使用視圖模型的應用
  • 胡克各應用視圖模型
  • 每次的INotifyPropertyChanged的情況下,您添加一個新的應用程序,也添加監聽器到這個應用程序

如果你只是想添加一個應用程序時做一些操作或刪除,您不必使用ViewModel,只需註冊ObservableCollection的CollectionChanged事件

+0

好吧,這清除了一些東西。不過我想知道如果你使用ViewModel來包含一些你肯定依賴於Facade模式的組合屬性,對嗎?這難道不會使可維護性更加複雜,並迫使您編寫重複代碼以訪問您不想隱藏的相同屬性? –

+0

對於我當前的項目,即時通訊使用ViewModels的DynamicObject包裝,我只是想添加一些屬性或視圖依賴功能。這不是最快的方法,但非常方便,因爲我不必爲屬性複製代碼,只需將Model分配給ViewModel即可。 對於我的模型,我沒有辦法註冊到更改,因爲它沒有實現INotifyPropertyChanged接口,但只保存數據。 – Spongebrot