[英]Should i create a Class for each button in the interface?
I have a question, i watched a lot of tutorial about MVVM but i'm still confused. 我有一个问题,我看了很多有关MVVM的教程,但我仍然很困惑。 I have an interface with more than one button on it, i have to implement the ICommand interface for bind the command to the View. 我有一个带有多个按钮的接口,我必须实现ICommand接口才能将命令绑定到View。
I did it like this: 我这样做是这样的:
MainSolution 主要解决方案
Main Solution
Model
SomeClass.cs
ViewModel
Commands
SomeCommands.cs
SomeViewModel.cs
Ok, now in the interface i have more than one button, each one do separate things, example, one is for start a thread the other one for cancel it and a third one for another thing. 好的,现在在界面中,我有多个按钮,每个按钮分别做一件事情,例如,一个用于启动线程,另一个用于取消线程,第三个用于另一件事。 Should i create a separate class, that implement ICommand interface, for each button that i have on the View? 我应该为View上的每个按钮创建一个单独的类来实现ICommand接口吗?
Main Solution
Model
SomeClass.cs
ViewModel
Commands
StartCommands.cs
CancelCommands.cs
OtherCommands.cs
SomeViewModel.cs
I'm asking this because when i implement the ICommand interface i have only one "Execute" method, and only one "CanExecute" method. 我之所以这样问是因为,当我实现ICommand接口时,我只有一个“ Execute”方法,只有一个“ CanExecute”方法。 What is the common way to implement multiple button on a View through binding? 通过绑定在视图上实现多个按钮的常用方法是什么?
I searched for example online without any luck... A lot of them are so confusing, and surely not for a newbie like me. 例如,我在网上搜索时没有任何运气。。。其中很多都如此令人困惑,而且肯定不是像我这样的新手。
The other thing is when i have multiple View and Multiple ViewModel, should i create multiple Commands Folder for nesting it? 另一件事是当我有多个View和多个ViewModel时,是否应该创建多个Commands文件夹进行嵌套?
Main Solution
Model
SomeClass.cs
ViewModel
FirstCommands
StartCommands.cs
CancelCommands.cs
OtherCommands.cs
SecondCommands
StartCommands.cs
CancelCommands.cs
OtherCommands.cs
FirstViewModel.cs
SecondViewModel.cs
IMHO having a separate class for each command in a classic MVVM approach is an overkill. 恕我直言,在传统的MVVM方法中,每个命令都具有单独的类是过大的选择。 Use something like the MVVMLight's RelayCommand (and the generic one if you want to pass a parameter). 使用类似MVVMLight的RelayCommand (如果要传递参数,则使用通用命令 )。
Then you can define your commands as your ViewModel members and provide Execute
/ CanExecute
implementation as part of your ViewModel as well: 然后,可以将命令定义为ViewModel成员,并提供Execute
/ CanExecute
实现作为ViewModel的一部分:
public RelayCommand MyCommand { get; }
public MyView()
{
//[...]
this.MyCommand = new RelayCommand(this.ExecuteMyCommand, this.CanExecuteMyCommand);
}
public void ExecuteMyCommand()
{
//do work!
}
public bool CanExecuteMyCommand()
{
//optional - control if command can be executed at a given time
}
And you can bind to MyCommand
in XAML (assuming your View is bound to your ViewModel): 您可以在XAML中绑定MyCommand
(假设您的View已绑定到ViewModel):
<Button Content='WORK!' Command='{Binding MyCommand}' />
What you want is a DelegateCommand
class. 您想要的是一个DelegateCommand
类。 You can use Prism , or just google around and steal one; 您可以使用Prism ,也可以只是在Google周围搜索并窃取一个。 here's one on StackOverflow that I haven't tested but it looks legit. 这是我尚未测试过的StackOverflow上的一个,但看起来合法。 If you don't need a parameter for a command, just ignore the parameter
argument. 如果命令不需要参数,则忽略parameter
参数。
A DelegateCommand
implements ICommand
, and calls an Action
or Action<object>
that you pass to its constructor, like so (from the StackOverflow answer linked above): DelegateCommand
实现ICommand
,并调用传递给其构造函数的Action
或Action<object>
,就像这样(来自上面链接的StackOverflow答案):
public DelegateCommand AddFolderCommand { get; protected set; }
public MyViewModel(ExplorerViewModel explorer)
{
AddFolderCommand = new DelegateCommand(ExecuteAddFolderCommand, (x) => true);
}
public void ExecuteAddFolderCommand(object param)
{
MessageBox.Show("this will be executed on button click later");
}
This should have been included in WPF, but wasn't, for whatever reason. 它应该已经包含在WPF中,但是出于某种原因却没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.