流星

流星飞过的刹那,我....
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WPF中的命令(commands)

Posted on 2009-02-17 10:21  泥土  阅读(1321)  评论(0编辑  收藏  举报

      在WPF中,命令(commands)是实现共享动作的一种手段,在应用程序中这些动作可以被分组并且可以以几种不同的方式触发。在大多数WPF应用程序中,大量的经常需要的功能,可以通过窗口上的具有快捷键的菜单项、按钮、各种控件或应用程序逻辑在任何位置聊用。这些项目中的每一个都可以与可执行逻辑相关联以执行这些通常的操作。为了减少冗余,WPF为开发人员提供了命令。

      使用这些命令可以实现快捷键Alt+F,Ctrl+V之类的操作。这种使用命令的逻辑集中对于彻底解决应用程序接口设计来说是很重要的。

      命令被认为是传递的,其部分原因是他们跟随了WPF中事件下传和上传的路线。每次命令被执行,都需要钩住它的下传和上传事件。当在应用程序中使用RoutedCommand类时,它将引发PreviewExecuteEvent和ExecuteEvent事件。PreviewExecuteEvent提供了一个下传事件来抢先处理还未到达的事件,相反,ExecuteEvent提供了相应的上传事件来访问紧跟在时间触发之后的逻辑。此外,RoutedCommand类还提供了一个可重载的Execute方法以供使用,可以将一个特定的目标元素作为参数传递给该方法以提供上下文。

      下面是WPF中用于传递命令的4个不同的类:

描 述

ApplicationCommands

包含公共的剪切板和文件级的命令,例如File|Open、File|New等。

ComponentCommands

允许向上或向下滚动、移动到文本选择的头部或移动到文本选择的尾部

EditCommands

提供编辑功能,例如字体的格式化等

MediaCommands

提供与多媒体相关的操作,例如播放、停止等。

      关于命令的具体操作可以查看MSDN.

 

声明命令:

XAML标记:

<Window x:Class="Commands.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">
    <Grid>
        <StackPanel>
            <StackPanel.CommandBindings>
                <CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Executed"></CommandBinding>
            </StackPanel.CommandBindings>
            <Button Command="ApplicationCommands.Paste">Paste something(control + v ) when i have focus !</Button>
        </StackPanel>
    </Grid>
</Window>

其中,CommandBinding类用于为在窗口或应用程序中的命令执行提供特定用户界面元素的上下文。首先,CommandBinding类与具有RoutedCommand对象的用户界面控件的一个事件处理程序相连接,然后,RoutedCommand类的对象引发PreviewExecute和Execute事件,从而进入创建的命令处理程序。CommandBinding类实际上一共提供了4个事件:Execute,PreviewExecute,QueryEnabled,PreviewQueryEnabled.

c#后台代码:

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

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

定义新的命令:

 

<Window x:Class="Commands2.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">
    <Grid>
        <Button Height="23" HorizontalAlignment="Left" Margin="53,70,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Button</Button>
    </Grid>
</Window>

 

其中.Button也可以用来调用命令

 

C#后台代码:

   public partial class Window1 : Window
   {
       public static RoutedCommand myCmd;//声明一个静态命令对象

       public Window1()
       {
           InitializeComponent();

           myCmd = new RoutedCommand();//命令初始化

           CommandBinding customCommandBinding = new CommandBinding(myCmd, ExecutedCustomCommand, CanExecuteCustomCommand);//将RoutedCommand绑定到实现该命令的事件处理程序
           this.CommandBindings.Add(customCommandBinding); //将CommandBinding添加到CommandBindingCollection

           InputGesture input = new KeyGesture(Key.G, ModifierKeys.Control | ModifierKeys.Shift); //定义可用来调用命令的组合键   Ctrl+Shift+G
           myCmd.InputGestures.Add(input);//将组合键和命令对象关联          

       }       
       public void ExecutedCustomCommand(object sender, ExecutedRoutedEventArgs e) //RoutedCommand对象的Executed事件处理程序
       {
           MessageBox.Show("custom command excuted");
       }
       public void CanExecuteCustomCommand(object sender, CanExecuteRoutedEventArgs e) //RoutedCommand对象的CanExecuted事件处理程序
       {
           Control target = e.Source as Control;
           if (target != null)
           {
               e.CanExecute = true;
           }
           else
           {
               e.CanExecute = false;
           }
       } 
       
       private void button1_Click(object sender, RoutedEventArgs e)//鼠标调用命令
       {
           myCmd.Execute(sender, null);
       }
   }