XAML 中的命令绑定与 ViewModel 中的 ICommand 属性

发布于 2024-11-18 11:06:52 字数 1229 浏览 2 评论 0原文

我刚刚开始在应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-11-25 11:06:52

这取决于你的设计。如果您打算采用快速方法 - 具有后端代码的窗口,那么从长远来看,在 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.

菊凝晚露 2024-11-25 11:06:52

我认为它们之间的主要区别在于第一个版本的路由性质。路由方面可以使命令在某些情况下更加强大,但也可能会带来更多痛苦。如果您尝试执行命令,但目标 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文