WPF中的命令简介

使用Prism委托命令Demo: WPF委托命令DelegateCommand的传参方式 

在WPF中使用命令的步骤很简单

1.创建命令

2.绑定命令

3.设置命令源

4.设置命令目标

WPF中命令的核心是System.Windows.Input.ICommand接口,所有命令对象都实现了此接口。当创建自己的命令时,不能直接实现ICommand接口,而是要使用System.Windows.Input.RouteCommand类,该类已经实现了ICommand接口,所有WPF命令都是RouteCommand类的实例。在程序中处理的大部分命令不是RoutedCommand对象,而是RoutedUICommand类的实例,它继承自RouteCommand类。

WPF提供了一个命令库,命令库中提供了多个常用的命令,命令库通过5个专门的静态类的静态属性来提供。

5个静态类分别为:

ApplicationCommands 该类提供通用命令,包括Copy、Cut、Paste等等。

NavigationCommands 该类提供了导航的命令。

EditingCommands 该类提供了很多主要的文档编辑命令 如MoveToLineEnd、MoveLeftByWord等等。

CommponentCommands 该类提供了用户界面元素使用的命令。

MediaCommands 该类提供了一组用于处理多媒体的命令。

MSDN帮助文档搜索ApplicaiontCommands、NavigationCommands等可以看到命令详细用法

通过上面5个静态类的静态属性可以获得常用的命令对象。

下面的XAML示例演示了将命令源关联到按钮,代码如下。

<Window x:Class="WPF命令详解.Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen"
         Loaded="Window_Loaded">
      <Grid>
         <Grid.RowDefinitions>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
         </Grid.RowDefinitions>
         <Button Grid.Row="0" Command="ApplicationCommands.Open"></Button>
         <Button Grid.Row="1" Command="ApplicationCommands.New"></Button>
         <Button Grid.Row="2" Command="ApplicationCommands.Save"></Button>
         <Button Grid.Row="3" Command="ApplicationCommands.Copy"></Button>
     </Grid>
</Window>

 

从上面的代码中可以看到,通过Command关联命令对象,当应用程序执行时,会发现按钮都是不可用的,变成了不可用状态与IsEnable属性设置为False一样。这是因为按钮还没有关联绑定,下面看一下关键绑定后的代码如下。

XAML代码如下。

<Window x:Class="WPF命令详解.Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen"
         Loaded="Window_Loaded">
     <Window.CommandBindings>
         <CommandBinding Command="ApplicationCommands.Copy"
                         Executed="CommandBinding_Executed">
         </CommandBinding>
     </Window.CommandBindings>
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
         </Grid.RowDefinitions>
         <Button Grid.Row="0" Command="ApplicationCommands.Open"></Button>
         <Button Grid.Row="1" Command="ApplicationCommands.New"></Button>
         <Button Grid.Row="2" Command="ApplicationCommands.Save"></Button>
         <Button Grid.Row="3" Command="ApplicationCommands.Copy"></Button>
     </Grid>
</Window>

 

CS代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF命令详解
{
     /// <summary>
     /// Window1.xaml 的交互逻辑
     /// </summary>
     public partial class Window1 : Window
     {
         public Window1()
         {
             InitializeComponent();
         }

         private void Window_Loaded(object sender, RoutedEventArgs e)
         {
             CommandBinding binding = new CommandBinding(ApplicationCommands.New);
             binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
             CommandBinding cmd_Open = new CommandBinding(ApplicationCommands.Open);
             cmd_Open.Executed += new ExecutedRoutedEventHandler(cmd_Open_Executed);
             CommandBinding cmd_Save = new CommandBinding(ApplicationCommands.Save);
             cmd_Save.Executed += new ExecutedRoutedEventHandler(cmd_Save_Executed);


             this.CommandBindings.Add(binding);
             this.CommandBindings.Add(cmd_Open);
             this.CommandBindings.Add(cmd_Save);
         }

         void cmd_Save_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             MessageBox.Show("保存");
         }

         void cmd_Open_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             MessageBox.Show("打开");
         }

         void binding_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             MessageBox.Show("新建");
         }

         private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             MessageBox.Show("复制");
         }
     }
}

 

从上面的代码中可以看到,在XAML代码中可以实现命令绑定。

    

 <Window.CommandBindings>
         <CommandBinding Command="ApplicationCommands.Copy"
                         Executed="CommandBinding_Executed">
         </CommandBinding>
 </Window.CommandBindings>

 


也可以在CS代码中实现命令绑定。

            

 CommandBinding binding = new CommandBinding(ApplicationCommands.New);
             binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
             this.CommandBindings.Add(binding);


还有就是要写Executed事件中的代码。

        

 void binding_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             MessageBox.Show("新建");
         }

上面的内容是通过实现了ICommandSource接口的Button控件来触发执行的命令,下面演示了直接调用命令的方式,代码如下。

ApplicationCommands.Open.Execute(null, this);
CommandBindings[0].Command.Execute(null);

第一种方法使用了命令对象的Execute方法调用命令,此方法接收两个参数,第一个参数是传递的参数值,第二个参数是命令绑定的所在位置,示例中使用了当前窗体。

第二种方法在关联的CommandBinding对象中调用Execute方法,对于这种情况不需要提供命令绑定的所在位置,因为会自动将提供正在使用的CommandBindings集合的元素设置为绑定位置。

posted @ 2017-08-10 08:26  致林  阅读(4515)  评论(0编辑  收藏  举报