2

ViewModel を作成する場合、MVVM パターンで Model を作成する必要があるというのは本当ですか?

たとえば、ウィンドウの上部にある WPF アプリで簡単なメニューを作成するという私のタスク。

これは私のView 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のViewModelです

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

ビューにバインドされている MainWindowViewModel.cs基本クラスである私のクラスWorkspaceViewModel.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 クラスのモデルを作成して別の方法で行う必要がありますか?

4

1 に答える 1

2

はいといいえ。

MVVM パターンは、その性質上、Model を持つことを規定していますが、これは、MVVM を完全に実装する必要があるという意味ではありません。

何をするかは、プロジェクトと MVVM の理解 (または解釈) によって異なります。私は以前、MVVM と MVC を組み合わせたアプローチを取っていました。つまり、V/VM とその背後にあるコントローラーがありました (コントローラーはほとんど超大型モデルのようなものです)。非常に単純なアプリケーションを使用している場合、単純にパターンの純粋さのために複雑にする意味はありません。目的に合う場合は、ビューとビューモデルだけを使用してください。

覚えておくべきことは、パターンは単に何かを行う方法のレシピまたは処方箋であるということです. 厳密に守らなければならないというルールはありません。多くの場合、それができない場合があり、そのためパターンを適応させるか、複数のパターンを使用する必要があります。重要なことは、行うことの一貫性を維持することです。

于 2013-02-04T01:10:15.897 に答える