WPF中的命令(Command)
来源:互联网 发布:梦回传奇单机修改数据 编辑:程序博客网 时间:2024/06/05 10:28
WPF中的命令系统由几个基本要素构成,它们是:
命令(Command):WPF的命令实际上就是实现了ICommand接口的类,平时使用最多的是RoutedCommand类。ICommand接口在程序集System.dll中,命令空间为System.Windows.Input;语法[TypeConverterAttribute("System.Windows.Input.CommandConverter,PresentationFramework, Version=4.0.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35, Custom=null")]
public interface ICommand
方法
名称
说明
CanExecute
定义用于确定此命令是否可以在其当前状态下执行的方法。
Execute
定义在调用此命令时调用的方法。
事件
名称
说明
CanExecuteChanged
当出现影响是否应执行该命令的更改时发生。
命令源(CommandSource):
即命令的发送者,是实现了ICommandSource接口的类。ICommandSource 接口在程序集PresentationCore.dll中,命令空间为:System.Window.Input;语法public interface ICommandSource属性
名称
说明
Command
获取将在调用命令源时执行的命令。
CommandParameter
表示可在执行命令时传递给该命令的用户定义的数据值。
CommandTarget
将在其上执行命令的对象。
命令目标(Command Target):即命令将发送给谁,或者说命令将作用在谁身上。命令目标必须是实现IInputElement接口的类。IInputElement 接口在程序集PresentationCore.dll中,命令空间为:System.Windows;作用建立公用事件和事件相关的属性和方法处理的Windows Presentation Foundation(WPF)元素的基本输入的。命令关联(Command Binding):负责把一些外围逻辑与命令关联起来,比如执行之前对命令是否可以执行进行判断、命令执行之后还有哪些后续工作等。WPF需要CommandBinding在执行前来帮助判断是不是可以执行、在执行后做一些事件来“打扫战场”。CommandBinding类在程序集PresentationCore.dll中,命令空间是System.Windows.Input;作用将RoutedCommand绑定到实现该命令的事件处理程序。构造
名称
说明
CommandBinding()
初始化 CommandBinding 类的新实例。
CommandBinding(ICommand)
使用指定的 ICommand 初始化 CommandBinding 类的新实例。
CommandBinding(ICommand, ExecutedRoutedEventHandler)
使用指定的 ICommand 和指定的Executed 事件处理程序初始化 CommandBinding 类的新实例。
CommandBinding(ICommand, ExecutedRoutedEventHandler, CanExecuteRoutedEventHandler)
使用指定的 ICommand 和指定的Executed 及 CanExecute 事件处理程序初始化 CommandBinding 类的新实例。
属性
名称
说明
Command
获取或设置与此 CommandBinding 关联的 ICommand。
事件
名称
说明
CanExecute
当与该 CommandBinding 关联的命令启动检查以确定是否可以在命令目标上执行此命令时发生。
Executed
当执行与该 CommandBinding 关联的命令时发生。
PreviewCanExecute
当与该 CommandBinding 关联的命令启动检查以确定是否可以在当前命令目标上执行此命令时发生。
PreviewExecuted
当执行与该 CommandBinding 关联的命令时发生。
以下是一个使用RoutedCommand命令的实例:
MainWindow.xmal
<Window x:Class="CommandFirstTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Command" Height="175" Width="260" Background="LightBlue"> <StackPanel x:Name="stackPanel"> <Button x:Name="button1" Content="Send Command" Margin="5"/> <TextBox x:Name="textBoxA" Margin="5,0" Height="100"/> </StackPanel></Window>
MainWindow.xmal.cs
using System;using System.Windows;using System.Windows.Input;namespace CommandFirstTest{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //把命令赋值给命令源(发送者),并指定快捷键 this.button1.Command = clearCmd; this.clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt)); //指令命令目标 this.button1.CommandTarget = this.textBoxA; //创建命令关联 CommandBinding cb = new CommandBinding(); cb.Command = this.clearCmd; //只关注与clearCmd命令相关的事件 cb.CanExecute += cb_CanExecute; cb.Executed += cb_Executed; //把命令安置在外围控件上 this.stackPanel.CommandBindings.Add(cb); } /// <summary> /// 当命令送达目标后,此方法被调用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void cb_Executed(object sender, ExecutedRoutedEventArgs e) { this.textBoxA.Clear(); //避免继续向上传而影响程序性能 e.Handled = true; } /// <summary> /// 探测命令是否可执行时,此方法被调用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (string.IsNullOrEmpty(this.textBoxA.Text)) e.CanExecute = false; else e.CanExecute = true; //避免继续向上传而影响程序性能 e.Handled = true; Console.WriteLine("这个事件会一直被监听"); } /// <summary> /// 声明并自定义命令 /// </summary> private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof (MainWindow)); }}示例来自:深入浅出WPF (176页 - 179页) 9.1 命令系统的基本元素与关系
参考:MSDN
- WPF中的命令(Command)
- WPF中的Command命令
- WPF 自定义快捷键命令(Command)(转)
- WPF 自定义快捷键命令(Command)
- WPF自定义命令Command
- WPF中的Command
- WPF/Silverlight中的Command
- WPF/Silverlight中的Command
- WPF中的命令简述
- WPF中的命令简述
- WPF中的命令
- wpf command
- WPF command
- WPF中的命令(一)- 使用命令的步骤
- WPF中的命令(二)- 命令中传递参数
- WPF中的命令邦定
- WPF系统中的命令系统
- 生活中的设计模式之命令(Command)模式
- 募集性能测试好手!
- 免费的国外图像库,数字图像处理必备
- Illustrator插件开发之ADM列表
- AS与JS的相互调用
- 为什么要学习C语言?
- WPF中的命令(Command)
- 用ExpandableListAdapter在每个child列表前添加图片
- To 薛洪2
- V8 源码 win 系统下编译方法以及 V8 dll 化方法
- Qt5.1.1 for Android
- java_web 学习第一天(Servlet入门)
- 黑马程序员————GUI(day22)
- ZOJ 2016 欧拉路判定
- snprintf函数使用(Windows与Linux版本)