每个 class 的单独 MVVM 命令

Separate MVVM command to each class

我是MVVM模式的新手。经过长时间的谷歌搜索,我从一个按钮的 MVVM 命令开始。为了更好,我使用 MVVM Light NuGet 包来编写代码。有 2 个按钮,点击每个按钮我都会做一些事情(在这里,点击按钮 A 显示 'Implement feature A')。下面的代码工作正常:

namespace ButtonMVVM
{
    class CommandViewModel : ViewModelBase
    {
        public RelayCommand FeatureA { get; private set; }
        public RelayCommand FeatureB { get; private set; }

        public CommandViewModel()
        {
            this.FeatureA = new RelayCommand(this.FeatureAFunc);
            this.FeatureB = new RelayCommand(this.FeatureBFunc);
        }

        private void FeatureAFunc()
        {
            MessageBox.Show("Implement feature A");
        }

        private void FeatureBFunc()
        {
            MessageBox.Show("Implement feature B");
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

并且在 xaml 文件中:

<Window x:Class="ButtonMVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ButtonMVVM"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:CommandViewModel/>
    </Window.DataContext>

    <Grid>
        <Button Command='{Binding FeatureA}' x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="54,81,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Command='{Binding FeatureB}' x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="179,81,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

但是现在,假设我有很多功能(即 10 个功能),并且有很多代码,每个功能都有很多辅助方法。我不能把它们全部写在 1 class CommandViewModel.

有没有办法将每个特征放入指定的 class(即 featureA.csfeatureB.cs...)?

如果您只想拥有多个文件,您可以创建 CommandViewModel class partial 并为单个 class 创建多个文件。然后,将每个特征放入对应的文件中:

CommandViewModel.cs:

partial class CommandViewModel : ViewModelBase
{
    public CommandViewModel()
    {
        this.FeatureA = new RelayCommand(this.FeatureAFunc);
        this.FeatureB = new RelayCommand(this.FeatureBFunc);
    }
}

CommandViewModel.FeatureA.cs:

partial class CommandViewModel
{
    public RelayCommand FeatureA { get; }

    private void FeatureAFunc()
    {
        MessageBox.Show("Implement feature A");
    }
}

CommandViewModel.FeatureB.cs:

partial class CommandViewModel
{
    public RelayCommand FeatureB { get; }

    private void FeatureBFunc()
    {
        MessageBox.Show("Implement feature B");
    }
}

从技术上讲,这仍将是一个 class,但会分成多个文件。另一个缺点是您将只有一个构造函数,因此您必须将所有功能的初始化逻辑放在一个文件中。这样做的好处是你不需要触摸你的 XAML.

另一种变体:使用多个视图模型,为每个特征指定一个。

MainViewModel.cs:

class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        this.FeatureA = new ViewModelFeatureA();
        this.FeatureB = new ViewModelFeatureB();
    }

    public ViewModelFeatureA FeatureA { get; }
    public ViewModelFeatureB FeatureB { get; }
}

ViewModelFeatureA.cs:

class ViewModelFeatureA : ViewModelBase
{
    public ViewModelFeatureA ()
    {
        this.FeatureACommand = new RelayCommand(this.FeatureAFunc);
    }

    public RelayCommand FeatureACommand { get; }

    private void FeatureAFunc()
    {
        MessageBox.Show("Implement feature A");
    }
}

这将使您能够将逻辑封装在不同的 classes 和文件中。但是,您需要更改视图的绑定。

通过主视图模型的属性访问视图模型:

<Button Command='{Binding FeatureA.FeatureACommand}'/>

或更改相关视图部分的数据上下文:

<Grid DataContext="{Binding FeatureA}>
    <Button Command='{Binding FeatureACommand}'/>
</Grid>