1. 程式人生 > >WPF中的Command命令詳解

WPF中的Command命令詳解

cti system tle command location eric 自己的 pri edit

在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 該類提供了一組用於處理多媒體的命令。

通過上面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集合的元素設置為綁定位置。

WPF中的Command命令詳解