2023年7月19日发(作者:)
WPF命令(RoutedCommand⾃定义命令,实现ICommand接⼝⾃定义命令)。推。。。强烈推荐阅读⼀、命令基本元素及关系 我们已经知道WPF⾥已经有了路由事件,可以发布及传播⼀些消息,那为什么还需要命令呢?这是因为事件指负责发送消息,对消息如何处理则不管,⽽命令是有约束⼒,每个接收者对命令执⾏统⼀的⾏为,⽐如菜单上的保存,⼯具栏上的保存都必须是执⾏同样的保存。
在WPF中,命令(Commanding)被分割成了四个部分,分别是ICommand,ICommandSource,CommandTarget和CommandBinding。下⾯我们来分别探讨这四个部分。 命令(Command):实现了ICommand接⼝的类,经常使⽤的有RoutedCommand类 (private RoutedCommand clearCmd = newRoutedCommand("序列化的声明名称", typeof(所有者类型));) 命令源:是命令的发送者,是实现了ICommandSource接⼝的类,⼤部分界⾯的控件都实现了这个接⼝,Button, MenuItem 等等。 (为按钮设置调⽤的命令 如:按钮.Command=) 命令⽬标:命令的接收者,命令⽬标是视线了IInputElement接⼝的类。 (设置引发指定命令的元素 如:按钮.CommandTarget= ) 命令关联:负责⼀些逻辑与命令关联起来,⽐如判断命令是否可以执⾏,以及执⾏完毕后做⼀些处理。 1、CommandBinding :将RoutedCommand 绑定到事件处理程序。 ( //CommandBinding 将命令绑定到事件处理程序 CommandBinding cb = new CommandBinding(); //命令 d = 命令对象; //事件处理程序 cute += new CanExecuteRoutedEventHandler(cb_CanExecute); ed += new ExecutedRoutedEventHandler(cb_Executed); ) 2、把命令关联安置在外围控件上(将命令CommandBinding添加到命令集合CommandBindings 中) 布局控件.(CommandBinding对象);四个命令元素之间的关系:
实例⼀:RoutedCommand⾃定义命令: 与业务逻辑⽆关的命令,使⽤ RoutedCommand,业务逻辑要依靠外围的CommandBinding来实现。这样⼀来,如果对CommandBinding管理不善就可能造成代码混乱⽆章,毕竟⼀个CommandBinding要牵扯到谁是它的宿主以及它的两的事件处理器。
使⽤ Button 来发送这个命令,当命令送达到 TextBox 时,TextBox被清空(如果TextBox没有⽂字,则命令不可以被发送)效果:
代码:
using System;using c;using ;using ;using s;using ls;using ;using nts;using ;using ;using g;using ;namespace WpfApplication{ ///
cute+=new CanExecuteRoutedEventHandler(cb_CanExecute); ed+=new ExecutedRoutedEventHandler(cb_Executed); //第五步:把命令关联安置在外围控件上(将命令绑定添加到命令集合中) (cb); } //当探测命名是否可以执⾏时,此⽅法被调⽤ public void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e) public void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (OrEmpty()) { cute = false; } else { cute = true; } //避免继续向上传⽽降低程序性能 d = true; } //命令送达⽬标后,此⽅法被调⽤ public void cb_Executed(object sender, ExecutedRoutedEventArgs e) { (); //避免继续向上传⽽降低程序性能 d = true; } }}推荐阅读:======================================================================================== public partial class Window13 : Window { //第1步:定义路由命令 private RoutedCommand clearCmd = new RoutedCommand(); public Window13() { InitializeComponent(); //第2步:为按钮指定【命令】 d = clearCmd; //第3步:为按钮指定【命令⽬标】 dTarget = xA; //第4步:将路由命令绑定到事件处理程序 var cb = new CommandBinding(clearCmd, ClearExcute, ClearCanExcute); //第5步:将路由命令绑定添加到命令集合中 (cb); } ///
实例三:命令中的参数传递(同个命令区分,需要参数)
代码:
⼆、近观命令1、ICommand接⼝与RoutedCommand
2、⾃定义命令(实现 ICommand 接⼝) (推荐) 命令:实现 ICommand 接⼝开始,定义⾃⼰的命令并且把某些业务逻辑包含到命令中,这才是真正意义上的⾃定义命令。 命令源: 实现 ICommandSource接⼝ 命令⽬标:
第⼀步:定义接⼝ ,并且需要接受命令的控件都要实现这个接⼝,这样就确保了命令可以成功对它执⾏操作。
public interface IView { bool IsChanged { get; set; } void SetBinding(); void Refresh(); void Clear(); void Save(); }第⼆步:创建命令 。
//命令 //实现ICommand接⼝,并继承了CanExecuteChanged事件,CanExecute⽅法和Execute⽅法 public class ClearCommand:ICommand { //当命令可执⾏状态发⽣改变时,应当被激活 public event EventHandler CanExecuteChanged; //⽤于判断命令是否可以执⾏(暂时不实现) public bool CanExecute(object parameter) { throw new NotImplementedException(); } public void Execute(object parameter) { //执⾏命令,带与业务相关的Clear逻辑 IView view = parameter as IView; if (view != null) { (); } } } 执⾏⽤户控件MiniView的Clear()⽅法
第三步:命令源(调⽤命令的对象 如:按钮)
//命令源 public class MyCommandSource:UserControl1,ICommandSource { //继承 ICommandSource 三个属性 public ICommand Command{get;set;} public object CommandParameter{get;set;} public Element CommandTarget{get;set;} protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { eLeftButtonDown(e); //命令作⽤于命令⽬标 if (dTarget != null) { e(CommandTarget); } } }
CommandTarget 命令⽬标 如:miniView
e 点击⿏标左键,执⾏中的Execute⽅法
第四步:命令⽬标
效果图:⽤户控件:
效果图:
代码:
public Window11() { InitializeComponent(); //声明命令 ClearCommand clearCommand = new ClearCommand(); //命令源命令 d = clearCommand; //命令源⽬标 dTarget = ew;
}
//注意:正规⽅法,应该把命令声明在静态全局的地⽅,供所有对象使⽤。
实例:MVVM为按钮添加事件
public class DelegateCommand : ICommand { public Action
public class ViewModel { public DelegateCommand BuildCommand { get; set; } public ViewModel() { BuildCommand = new DelegateCommand(); eCommand = new Action
taContext = new ViewModel();
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689721109a280968.html
评论列表(0条)