命令
基础点
元素:
命令: 平时最多使用RoutedCommand,自定义则要实现ICommand接口
命令源:命令发送者,要求实现了ICommandSource接口的类,界面元素多实现了
命令目标:命令发送给谁
命令关联:把命令关联起来。PreviewCanExecute=发生在执行命令是否符合执行的条件判方法前;CanExecute=执行命令是否符合执行的条件判方法;PreviewCanExecute=命令被执行前调用的方法;Executed=命令被执行后调用
PreviewCanExecute、CanExecute都是一直在背后运行,调用频繁,让我们看起来是实时的效果,所耗性能,在其方法体要加上e.Handled = true;
命令是一处声明,可处处使用
命令定义与使用(当文本框内容不为空时,按钮可用,点击按钮后可以清空文本)
public partial class command1 : Window
{
//定义命令
private RoutedCommand clearCmd = new RoutedCommand("clearCmd", typeof(command1));
public command1()
{
InitializeComponent();
initCommand();
}
//加载命令
private void initCommand()
{
//把命令赋给命令源,即发送者
this.button.Command = clearCmd;
//指定快捷键 Alt+C
this.clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));
//命令目标
this.button.CommandTarget = textBox;
//创建命令关联
CommandBinding cb = new CommandBinding();
cb.Command = this.clearCmd;//只关注与clearCmd命令相关的事件
cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
//把命令关联安置在外围控件上,一定要在外围上,否则CanExecute、Executed无法被执行
grid1.CommandBindings.Add(cb);
}
//当命令发送目标后,此方法会被执行
private void cb_Executed(object sender, ExecutedRoutedEventArgs e)
{
this.textBox.Clear();
//避免继续往上传递降低程序性能 (因为此方法在背后频繁地被调用的)
e.Handled = true;
}
//挡探测命令是否可以用时,此方法会被执行
private void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (!string.IsNullOrEmpty(textBox.Text))
{
//当文本框内容不为空时
e.CanExecute = true;
}
else {
e.CanExecute = false;
}
//避免继续往上传递降低程序性能
e.Handled = true;
}
}
预定命令(WPF命令)
CanelPrint Close ContextMenu copy CorrectionList Cut Delete Find Help New NotACommand Open Paste Print PrintPreview Properties Redo Replace Save SaveAs SelectAll Stop Undo
命令参数
多个命令触发同一个命令,因为一个命令是唯 一实例,可以通过命令参数区分
<Grid x:Name="grid1" >
<TextBox x:Name="textBox" />
<Button x:Name="button1" Command="New" CommandParameter="button1" />
<Button x:Name="button2" Command="New" CommandParameter="button2" >
<ListBox x:Name="listBox" />
</Grid>
<Window.CommandBindings>
<CommandBinding Command="New" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"></CommandBinding>
</Window.CommandBindings>
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (!string.IsNullOrEmpty(textBox.Text))
{
e.CanExecute = true;
}
else
{
e.CanExecute = false;
}
e.Handled = true;
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
string name = textBox.Text;
this.listBox.Items.Add("点击者" + e.Parameter.ToString());
}
一个元素只有一个Command属性,要有可变性可结合 Binding使用
<Button x:Name="button1" Command="{Binding Path=ppp,Source=ss}" CommandParameter="button1" />
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构