2013-02-04 42 views
2

是真的,如果我創建一個ViewModel我需要在MVVM模式中爲它創建模型?是真的,如果我創建一個ViewModel我需要在MVVM模式中爲它創建模型?

例如我的任務是在窗口頂部的WPF應用程序中創建簡單的菜單。

這是我的看法MainWindow.xaml

<Window x:Class="AppMvvm.View.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:AppMvvm.ViewModel" 
    Title="{Binding DisplayName}" Height="350" Width="525" 
    WindowStartupLocation="CenterScreen"> 

<Window.Resources> 
    <Style x:Key="MenuItemStyle"> 
     <Setter Property="MenuItem.Command" Value="{Binding Command}" /> 
    </Style> 
    <HierarchicalDataTemplate DataType="{x:Type vm:MenuItemViewModel}" 
           ItemsSource="{Binding Children}"> 
     <ContentPresenter Content="{Binding DisplayName}" /> 
    </HierarchicalDataTemplate> 
</Window.Resources> 

<DockPanel> 
    <DockPanel DockPanel.Dock="Top"> 
     <Menu ItemsSource="{Binding MenuItems}" ItemContainerStyle="{StaticResource MenuItemStyle}"/> 
    </DockPanel> 


</DockPanel> 
</Window> 

這裏是我的視圖模型MenuItemViewModel.cs

namespace AppMvvm.ViewModel 
{ 
    public class MenuItemViewModel : ViewModelBase 
    { 

     public MenuItemViewModel(string menuItemName, IList<MenuItemViewModel> children) 
     { 
      base.DisplayName = menuItemName; 
      this.Children = children; 
     } 

     public MenuItemViewModel(string menuItemName) 
      : this (menuItemName, children: null) 
     { 
     } 

     public MenuItemViewModel(string menuItemName, ICommand command) 
     { 
      base.DisplayName = menuItemName; 
      this.Command = command; 
     } 

     public IList<MenuItemViewModel> Children { get; private set; } 

     public ICommand Command { get; private set; } 
    } 
} 

我班WorkspaceViewModel.cs是基類爲MainWindowViewModel.cs這是綁定到視圖

namespace AppMvvm.ViewModel 
{ 
    using Helpers; 

    public abstract class WorkspaceViewModel : ViewModelBase 
    { 
     #region Fields 
     private IList<MenuItemViewModel> _menuItems; 
     private RelayCommand _closeCommand; 
     #endregion 

     #region MenuItems 
     public IList<MenuItemViewModel> MenuItems 
     { 
      get 
      { 
       if (_menuItems == null) 
        _menuItems = CreateMenuItems(); 
       return _menuItems; 
      } 
     } 

     List<MenuItemViewModel> CreateMenuItems() 
     { 
      return new List<MenuItemViewModel> 
       { 
        new MenuItemViewModel("File", CreateFileMenuItems()), 
        new MenuItemViewModel("Tools"), 
        new MenuItemViewModel("About") 
       }; 
     } 

     #region CreateFileMenuItems & Commands 
     List<MenuItemViewModel> CreateFileMenuItems() 
     { 
      return new List<MenuItemViewModel> 
       { 
        new MenuItemViewModel("New"), 
        new MenuItemViewModel("Exit", CloseCommand) 
       }; 
     } 

     public ICommand CloseCommand 
     { 
      get 
      { 
       if (_closeCommand == null) 
        _closeCommand = new RelayCommand(p => Close()); 
       return _closeCommand; 
      } 
     } 

     void Close() 
     { 
      Application.Current.Shutdown(); 
     } 
     #endregion 

     #endregion 
    } 
} 

它是正確的做這樣或我需要創建爲MenuItemViewModel類模型,並做另一種方式?

回答

2

是的,沒有。

MVVM模式的本質在於規定您將擁有一個Model,但這並不意味着您需要完全實現MVVM。

您決定做什麼取決於您的項目以及您對MVVM的理解(或解釋)。在MVVM和MVC相結合之前,我採取了一種方法 - 即它有一個V/VM,然後是一個坐在它後面的控制器(控制器幾乎就像一個超級模型)。如果你有一個非常簡單的應用程序,那麼沒有必要僅僅爲了圖形純度而使它變得複雜 - 如果適合你的目的,那麼只需要一個視圖和viewmodel。

要記住的是,一個模式只是一個配方或處方如何做某事。沒有規則說你應該嚴格遵守它,在很多情況下你可能會發現你不能,因此你必須修改一個模式或使用幾種模式 - 關鍵是要保持與你所做的一致。

相關問題