Wpf 如果我创建一个ViewModel,我需要在MVVM模式中为它创建模型,这是真的吗?

Wpf 如果我创建一个ViewModel,我需要在MVVM模式中为它创建模型,这是真的吗?,wpf,mvvm,model,menu,viewmodel,Wpf,Mvvm,Model,Menu,Viewmodel,如果我创建一个ViewModel,我需要在MVVM模式中为它创建模型,这是真的吗 例如,我的任务是在窗口顶部的WPF应用程序中创建简单菜单 这是我的视图main窗口.xaml <Window x:Class="AppMvvm.View.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx

如果我创建一个ViewModel,我需要在MVVM模式中为它创建模型,这是真的吗

例如,我的任务是在窗口顶部的WPF应用程序中创建简单菜单

这是我的视图main窗口.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; }
    }
}
名称空间AppMvvm.ViewModel
{
公共类MenuItemViewModel:ViewModelBase
{
公共MenuItemViewModel(字符串menuItemName,IList子项)
{
base.DisplayName=menuItemName;
这个。孩子=孩子;
}
公共MenuItemViewModel(字符串menuItemName)
:此(menuItemName,子项:null)
{
}
公共MenuItemViewModel(字符串menuItemName、ICommand命令)
{
base.DisplayName=menuItemName;
这个命令=命令;
}
公共IList子项{get;private set;}
公共ICommand命令{get;private set;}
}
}
My classWorkspaceViewModel.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
    }
}
名称空间AppMvvm.ViewModel
{
使用助手;
公共抽象类WorkspaceViewModel:ViewModelBase
{
#区域字段
私人IList_menuItems;
专用中继命令_closeCommand;
#端区
#区域菜单项
公共IList菜单项
{
得到
{
如果(_menuItems==null)
_menuItems=CreateMenuItems();
返回菜单项;
}
}
列表CreateMenuItems()
{
返回新列表
{
新建MenuItemViewModel(“文件”,CreateFileMenuItems()),
新MenuItemViewModel(“工具”),
新MenuItemViewModel(“关于”)
};
}
#区域CreateFileMenuItems和命令
列出CreateFileMenuItems()
{
返回新列表
{
新MenuItemViewModel(“新”),
新MenuItemViewModel(“退出”,关闭命令)
};
}
公共ICommand CloseCommand
{
得到
{
如果(_closeCommand==null)
_closeCommand=newrelaycommand(p=>Close());
返回关闭命令;
}
}
无效关闭()
{
Application.Current.Shutdown();
}
#端区
#端区
}
}
这样做正确吗?还是我需要为MenuItemViewModel类创建一个模型,然后用另一种方法来做?

是和否

MVVM模式本质上规定您将拥有一个模型,但这并不意味着您需要完全实现MVVM

具体决定做什么取决于您的项目,以及您对MVVM的理解(或解释)。我以前采用过一种方法,将MVVM与MVC相结合,即它有一个V/VM,然后是一个控制器(控制器几乎像一个超级复制器模型)。如果您有一个非常简单的应用程序,那么仅仅为了模式纯度而将其复杂化是没有意义的——如果符合您的目的,请只使用视图和视图模型

要记住的是,模式只是如何做某事的处方或处方。没有规则说你应该严格遵守它,在许多情况下,你可能会发现你不能,因此你要么适应一种模式,要么使用几种模式——关键是保持与你所做的保持一致