自定义 WPF 命令模式示例 [英] Custom WPF command pattern example

查看:19
本文介绍了自定义 WPF 命令模式示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一些 WPF 编程,但我从未得到过的一件事是命令模式.每个示例似乎都是内置的,编辑、剪切、粘贴.任何人都有自定义命令的最佳实践示例或建议?

I've done some WPF programing and one thing I never got was the command pattern. Every example seems to be for built in ones, edit, cut, paste. Anyone have an example or suggestion of best practice for custom commands?

推荐答案

啊哈!一个我可以回答的问题!首先,我应该提到,我个人发现在代码中定义和连接命令比在 XAML 中更容易.它允许我比所有 XAML 方法更灵活地连接命令的处理程序.

Ah ha! A question I can answer! Firstly, I should mention that I have personally found it easier to define and hook up commands in code rather than in XAML. It allows me to hook up the handlers for the commands a little more flexibly than an all XAML approach does.

您应该弄清楚您想要拥有哪些命令以及它们与什么相关.在我的应用程序中,我目前有一个用于定义重要应用程序命令的类,如下所示:

You should work out what commands you want to have and what they relate to. In my application, I currently have a class for defining important application commands like so:

public static class CommandBank
{
  /// Command definition for Closing a window
  public static RoutedUICommand CloseWindow { get; private set; }

  /// Static private constructor, sets up all application wide commands.
  static CommandBank()
  {
    CloseWindow = new RoutedUICommand();
    CloseWindow.InputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.Alt));
    // ...
  }

现在,因为我想将所有代码放在一起,所以对命令使用仅代码方法可以让我将以下方法放在上面的类中:

Now, because I wanted to keep the code all together, using a code only approach to Commands lets me put the following methods in the class above:

/// Closes the window provided as a parameter
public static void CloseWindowExecute(object sender, ExecutedRoutedEventArgs e)
{
  ((Window)e.Parameter).Close();
}

/// Allows a Command to execute if the CommandParameter is not a null value
public static void CanExecuteIfParameterIsNotNull(object sender, CanExecuteRoutedEventArgs e)
{
  e.CanExecute = e.Parameter != null;
  e.Handled = true;
}

那里的第二种方法甚至可以与其他命令共享,而无需我在整个地方重复.

The second method there can even be shared with other Commands without me having to repeat it all over the place.

一旦您定义了这样的命令,您就可以将它们添加到任何 UI 中.在下文中,一旦加载了 Window,我将命令绑定添加到 Window 和 MenuItem,然后使用循环将输入绑定添加到 Window 以对所有命令绑定执行此操作.传递的参数是 Window 本身,因此上面的代码知道要尝试关闭哪个 Window.

Once you have defined the commands like this, you can add them to any piece of UI. In the following, once the Window has Loaded, I add command bindings to both the Window and MenuItem and then add an input binding to the Window using a loop to do this for all command bindings. The parameter that is passed is the Window its self so the code above knows what Window to try and close.

public partial class SimpleWindow : Window
{
  private void WindowLoaded(object sender, RoutedEventArgs e)
  {
    // ...
    this.CommandBindings.Add(
      new CommandBinding(
        CommandBank.CloseWindow,
        CommandBank.CloseWindowExecute,
        CommandBank.CanExecuteIfParameterIsNotNull));

    foreach (CommandBinding binding in this.CommandBindings)
    {
       RoutedCommand command = (RoutedCommand)binding.Command;
       if (command.InputGestures.Count > 0)
       {
         foreach (InputGesture gesture in command.InputGestures)
         {
           var iBind = new InputBinding(command, gesture);
           iBind.CommandParameter = this;
           this.InputBindings.Add(iBind);
         }
       }
    }

    // menuItemExit is defined in XAML
    menuItemExit.Command = CommandBank.CloseWindow;
    menuItemExit.CommandParameter = this;
    // ...
  }

  // ....
}

我后来也有 WindowClosing 和 WindowClosed 事件的事件处理程序,我建议您使命令的实际实现尽可能小和通用.在这种情况下,如果有未保存的数据,我没有尝试放置试图停止窗口关闭的代码,而是将该代码牢牢地保存在 WindowClosing 事件中.

I then also later have event handlers for the WindowClosing and WindowClosed events, I do recommend you make the actual implementation of commands as small and generic as possible. As in this case, I didn't try to put code that tries to stop the Window closing if there is unsaved data, I kept that code firmly inside the WindowClosing event.

如果您有任何后续问题,请告诉我.:)

Let me know if you have any follow up questions. :)

这篇关于自定义 WPF 命令模式示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆