c# - 对每个类单独的 MVVM 命令

标签 c# xaml mvvm viewmodel mvvm-light

我是 MVVM 模式的新手。经过长时间的谷歌搜索,我从一个按钮的 MVVM 命令开始。为了更好,我使用 MVVM Light NuGet 包编写代码。有 2 个按钮,单击每个按钮我都会做一些事情(这里,单击按钮 A 会显示“实现功能 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 类 CommandViewModel .

有没有办法将每个功能放入指定的类(即 featureA.csfeatureB.cs ...)?

最佳答案

如果你只想拥有多个文件,你可以让你的 CommandViewModel类(class) partial并为该单个类创建多个文件。然后,将每个特征放入相应的文件中:

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

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

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

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

这将使您可以将逻辑封装在不同的类和文件中。但是,您需要更改 View 的绑定(bind)。

通过主视图模型的属性访问 View 模型:
<Button Command='{Binding FeatureA.FeatureACommand}'/>

或者更改相关 View 部分的数据上下文:
<Grid DataContext="{Binding FeatureA}>
    <Button Command='{Binding FeatureACommand}'/>
</Grid>

关于c# - 对每个类单独的 MVVM 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62211690/

相关文章:

c# - WPF ListView : Detect when listviewitem is selected and then check it

c# - WPF 动画结束时是否会触发任何事件?

c# - SemanticZoom 中的 UWP GridView ItemTemplateSelector 不适用

c# - 将数据绑定(bind)到 WPF 用户控件

c# - 将连接字符串传递给 ViewModel 构造函数

c# - 使用 CsvHelper,如果第一列的值等于 0,如何映射到第二列

c# - 如何将路由参数传递给 Controller ​​构造函数

c# - 检查属性是否具有指定的属性,然后打印它的值

c# - 根据角色限制用户可以做什么

javascript - 将语音转文本模块添加到 C# 机器人