XAML 中的命令绑定与 ViewModel 中的 ICommand 属性
我刚刚开始在应用程序中使用 MVVM 命令。我找到了很多例子,并在我的代码中尝试了两种方法。一些示例在 xaml 中具有命令绑定,如下所示:
<CommandBinding Command="local:MainWindow.OpenRecentFile"
Executed="{Binding OpenRecentFile_Executed}" />
...
<MenuItem Header="{x:Static culture:TextResource.RecentFilesMenuItem}"
Command="local:MainWindow.RecentFilesCommand" >
OpenRecentFile_Executed 是 ViewModel 中的一个方法,并且是一个静态 ICommand,如下所示:
public static readonly ICommand OpenRecentFile =
new RoutedCommand("Open Recent", typeof(MainWindow));
我还看到 ViewModel 上有一个 ICommand 类型的属性,该属性绑定到像这样查看:
<MenuItem Header="Close Current File"
Command="{Binding CloseCurrentFileCommand}"
CommandParameter="{TemplateBinding DataContext}"/>
在 ViewModel 中:
private ICommand closeCurrentFileCommand;
public ICommand CloseCurrentFileCommand
{
get
{
if (closeCurrentFileCommand == null)
{
closeCurrentFileCommand =
new RelayCommand(param => this.CloseCurrentCedarFile(param));
}
return closeCurrentFileCommand;
}
}
每种方法的优点/缺点是什么?
I am just starting to use commanding with MVVM in an application. I've found a number of examples and have tried it both ways in my code. Some examples have the command binding in the xaml like so:
<CommandBinding Command="local:MainWindow.OpenRecentFile"
Executed="{Binding OpenRecentFile_Executed}" />
...
<MenuItem Header="{x:Static culture:TextResource.RecentFilesMenuItem}"
Command="local:MainWindow.RecentFilesCommand" >
With OpenRecentFile_Executed being a method in the ViewModel and a static ICommand like so:
public static readonly ICommand OpenRecentFile =
new RoutedCommand("Open Recent", typeof(MainWindow));
I have also seen where there is a property on the ViewModel that is of type ICommand that is bound to in the View like so:
<MenuItem Header="Close Current File"
Command="{Binding CloseCurrentFileCommand}"
CommandParameter="{TemplateBinding DataContext}"/>
and in the ViewModel:
private ICommand closeCurrentFileCommand;
public ICommand CloseCurrentFileCommand
{
get
{
if (closeCurrentFileCommand == null)
{
closeCurrentFileCommand =
new RelayCommand(param => this.CloseCurrentCedarFile(param));
}
return closeCurrentFileCommand;
}
}
What are the benefits/drawbacks to each method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于你的设计。如果您打算采用快速方法 - 具有后端代码的窗口,那么从长远来看,在 XAML 中声明命令可能会节省您一些时间并减少工作量。
如果您要使用 MVVM 应用程序,那么我强烈建议绑定到 ICommand,因为命令通常是操作数据(打开/保存/编辑)的方法,这应该在 ViewModel 中定义。可能需要更多的努力取决于功能,但如果您正在开发更大的应用程序,MVVM 是一个很好的选择。
最终两者的工作原理是一样的,但重要的是你的设计和方法。
It depends on your design. If you're going for the quick approach - a Window with back-end code then declaring commands in XAML will probably save you some time and reduce the effort in the long run.
If you are going for a MVVM app then I would strongly suggest the binding to ICommand as commands in general are ways to manipulate your data (opening/saving/editing) and this should be defined in the ViewModel. Possibly more effort depends on the functionality but MVVM is a great way to go if you're doing a larger application.
In the end both will work the same but it's your design and approach which matter.
我认为它们之间的主要区别在于第一个版本的路由性质。路由方面可以使命令在某些情况下更加强大,但也可能会带来更多痛苦。如果您尝试执行命令,但目标 ui 元素没有焦点,那么就会遇到麻烦。
基于属性的 ICommand 实现将始终有效,因为命令调用和命令传递之间没有“路由”步骤。
我倾向于主要使用基于属性的命令,除非我的场景需要路由提供的功能。
I think the main difference between these is the routed nature of the first version. The routing aspect can make the command more powerful for some scenarios, but it can also cause some more pain. The pain can come into play if your trying to get the command to execute, but the target ui element does not have focus.
A property based ICommand implementation will always work because there is no "routing" step between command invocation and command delivery.
I tend to use mostly property based commands unless my scenario calls for the features that routing offers.