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

查看:130
本文介绍了自定义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自身,所以上面的代码知道什么窗口尝试和关闭。

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天全站免登陆