Cammand命令
命令
实现了ICammand接口,常用的是继承RoutedCommand子类
命令源
实现了ICommandSource接口
命令目标
实现了IInpurElement接口,常常是直接对UI控件进行扩展
命令多使用单件模式………Singletone Pattern
1.创建命令类
2.声明命令实例
3.指定命令源
4.指定命令目标
5.设置命令关联
CommandManager:
PreviewCanExecute , PreviewExecute
CanExecute,Executed
CommandSource有Command,
CommandSource不断的向CommandTarget发送CanExecute和Executed,
CommandTarget向CommandBinding发送路由事件,
CommandBinding捕捉并处理事件,向Command反馈。
//定义命令
private RoutedCommand clearCmd = new RoutedCommand("Clear",typeof(Window1));
...
private void InitializeCommand(){
//指定命令源---------给控件添加命令
this.button1.Command = this.clearCmd;
this.clearCmd = InputGesture.Add(new KeyGesture(Key.C,Modifierkeys.Alt));
//指定命令目标
this.button1.CommandTarget = this.textBoxA;
创建CommandBinding
CommandBinding cb = new CommandBinding();
cb.Command = this.clearCmd;
cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
//把CommandBinding设置在外围控件上
this.stackPanel.CommandBindings.Add(cb);
}
void cb_CanExecute(object sender , CanExecuteRoutedEventArgs e){
if(string IsNullOrEmpty(this.textBoxA.Text)){
e.CanExecute = false;
}else{ e.CanExecute = true}
e.Handled = true;
}
void cb_Executed(object sender , CanExecuteRoutedEventArgs e){
this.textBoxA.Clear();
e.Handled = true;
}
命令参数 CommandParamter—命令携带的信息
<Button ... Command = "New" CommandParamter = "Teacher"/>
<Button ... Command = "New" CommandParamter = "Student"/>
...
<Window.CommandBinds>
<CommandBinding Command = "New" CommandParamter = "Teacher" CanExecute = "New_CanExecute" Executed = "New_Executed"/>
</Window.CommandBinds>
...
private void New_Executed(object sender , CanExecuteRoutedEventArgs e){
if(e.Parameter.ToString() == "Teacher"){ ... }
if(e.Parameter.ToString() == "Student"){ ... }
}
命令与Binding结合…使命令的赋值更加灵活
<Button x:Name = "dynamicCmdBtn" Command = "{Binding Path=ppp,Source=sss}" Content = "Command" }
可以根据特定条件改变命令
ICommand接口:
1.CanExecute方法
2.Execute方法
3.CanExecuteChanged事件
ICommandSource接口
1.Command属性
2.CommandParamter属性
3.CommandTarget属性
RoutedCommand类…实现了Icommand接口的类
并未向CanExecute方法和Execute方法添加任何逻辑
RoutedCommand与业务逻辑无关
业务逻辑要依靠外围组件的CommandBinding来实现
尽量把逻辑卸载Execute方法内
自定义命令
1.声明定义自己的RoutedCommand实例
2.实现ICommand接口,实现自己的业务逻辑,实现ICommandSource接口
例:
//定义接口
public interface IView{
bool IsChanged {get;set;}
void SetBinding();
void Refresh();
void Clear();
void Save();
}
//实现ICommand接口,创建一个作用于IView的命令
public class ClearCommand : ICommand{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object Parameter){...};
public void Execute(object Parameter){
IView view = Paramter as IView;
if( view != null ){ view.Clear(); }
}
}
//实现ICommandSource接口
public class MyCommandSource : UserControl,ICommandSource{
//继承自 ICommandSource的三个属性
public ICommand Command{get;set;}
public object CommandParameter{get;set;}
public IInputElement CommandTarget{get;set;}
//实现点击事件
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e){
base.OnMouseLeftButtonDown(e);
if(e.CommandTarget != null){
this.Command.Execute(this.CommandTarget);
}
}
}
实现IView的子类时先编辑XAML,再实现IView接口
InitializeComponent();
...
ClearCommand clearCmd = new ClearCommand();
this.ctrlClear.Command = clearCmd;
this.ctrlClear.CommandTarget = this.mineView//minewView实现IView接口