我是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 个功能(,并且有很多代码,每个功能都有很多帮助程序方法。我不能把它们都写在一个类CommandViewModel
.
有没有办法将每个功能放入指定的类中(即featureA.cs
、featureB.cs
...(?
如果只想包含多个文件,则可以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。
另一种变体:使用多个视图模型,为每个特征指定一个视图模型。
主视图模型.cs:
class MainViewModel : ViewModelBase
{
public MainViewModel()
{
this.FeatureA = new ViewModelFeatureA();
this.FeatureB = new ViewModelFeatureB();
}
public ViewModelFeatureA FeatureA { get; }
public ViewModelFeatureB FeatureB { get; }
}
视图模型功能A.cs:
class ViewModelFeatureA : ViewModelBase
{
public ViewModelFeatureA ()
{
this.FeatureACommand = new RelayCommand(this.FeatureAFunc);
}
public RelayCommand FeatureACommand { get; }
private void FeatureAFunc()
{
MessageBox.Show("Implement feature A");
}
}
这将允许您将逻辑封装在不同的类和文件中。但是,您需要更改视图的绑定。
通过主视图模型的属性访问视图模型:
<Button Command='{Binding FeatureA.FeatureACommand}'/>
或者更改相关视图部件的数据上下文:
<Grid DataContext="{Binding FeatureA}>
<Button Command='{Binding FeatureACommand}'/>
</Grid>