一、前言

​ 事件的作用是发布和传播一些消息,消息送达接收者,事件的使命也就完成了,至于消息响应者如何处理发送来的消息并不做规定,每个接收者可以使用自己的行为来响应事件。即事件不具有约束力。

​ 命令就有约束力,不仅可以约束代码,还可以约束步骤逻辑。

二、WPF 的 命令系统

​ WPF 中,命令系统由以下元素构成:

  1. 命令(Command):实现 ICommand 接口。表示一个程序任务,并可跟踪该任务是否完成。

  2. 命令源(Commannd Source):命令的发送者。需实现 ICommandSource 接口。

  3. 命令目标(Command Target):命令的接收者。需实现 IInputElment 接口的类。

  4. 命令关联(Command Binding):负责把外围逻辑和命令关联起来。

​  WPF中提供了一组已定义命令,命令包括以下类:ApplicationCommandsNavigationCommandsMediaCommandsEditingCommands 以及ComponentCommands。 这些类提供诸如 CutBrowseBackBrowseForwardPlayStopPause 等命令。下面我们使用 ApplicationCommands 进行以下测试:

        // 命令执行具体操作
private void CommandBinding_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show($"New 命令被触发了,命令源是:{e.Source}");
}
  <Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_OnExecuted"></CommandBinding>
</Window.CommandBindings>
<StackPanel Margin="20">
<Button Margin="10" Height="50" Content="New" Command="New"></Button>
</StackPanel>

​ 点击按钮后,输出结果为:

New 命令被触发了,命令源是:System.Windows.Controls.Button: New

三、自定义命令

​ 例如,我们需要增加一个数据的查库与上传数据操作,那么需要实现 Query 和 Insert 命令,为了能够直接供 UI 使用,我们声明 RoutedUICommand 命令,具体如下:

    public class DatabaseCommands
{
private static RoutedUICommand _query;
private static RoutedUICommand _insert;
static DatabaseCommands()
{
InputGestureCollection inputs = new InputGestureCollection(); inputs.Add(new KeyGesture(Key.Q ,ModifierKeys.Control, "Ctrl+R"));
_query = new RoutedUICommand(
"Query", "Query", typeof(DatabaseCommands), inputs); inputs.Add(new KeyGesture(Key.D, ModifierKeys.Control, "Ctrl+D"));
_insert = new RoutedUICommand(
"Add", "Add", typeof(DatabaseCommands), inputs);
} public static RoutedUICommand Query
{
get { return _query; }
} public static RoutedUICommand Insert
{
get { return _insert; }
}
}

​ 命令实现与绑定到 UI 上的操作如下:

        // 命令执行具体操作
private void DatabaseCommandsQuery_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show($"Query 命令被触发了,命令源是:{e.Source}");
} private void DatabaseCommandsAdd_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show($"Add 命令被触发了,命令源是:{e.Source}");
}
 <Window.CommandBindings>
<CommandBinding Command="local:DatabaseCommands.Query" Executed="DatabaseCommandsQuery_OnExecuted"></CommandBinding>
<CommandBinding Command="local:DatabaseCommands.Insert" Executed="DatabaseCommandsAdd_OnExecuted">
</CommandBinding>
</Window.CommandBindings>
<StackPanel Margin="20">
<Button Margin="10" Height="50" Content="New" Command="New"></Button>
<Button Margin="10" Height="50" Content="Query" Command="local:DatabaseCommands.Query"></Button>
<Button Margin="10" Height="50" Content="Insert" Command="local:DatabaseCommands.Insert"></Button>
</StackPanel>

​ 当我们点击 Query 和 Insert 操作时,分别执行对应的操作。上述例子中,Command 的执行代码是在 XAML 中声明的,那我们把命令的执行代码进行代码后置,且为了满足不同的条件,我们声明一个 RelayCommand 基类进行封装,具体如下:

    /// <summary>
/// The base implementation of a command.
/// </summary>
abstract class CommandBase : ICommand
{
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { System.Windows.Input.CommandManager.RequerySuggested += value; }
remove { System.Windows.Input.CommandManager.RequerySuggested -= value; }
} /// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>
public void OnCanExecuteChanged()
{
System.Windows.Input.CommandManager.InvalidateRequerySuggested();
} /// <summary>
/// Defines the method that determines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
/// <returns>
/// true if this command can be executed; otherwise, false.
/// </returns>
public virtual bool CanExecute(object parameter)
{
return true;
} /// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
public void Execute(object parameter)
{
if (!CanExecute(parameter))
{
return;
}
OnExecute(parameter);
} /// <summary>
/// Executes the command.
/// </summary>
/// <param name="parameter">The parameter.</param>
protected abstract void OnExecute(object parameter);
} /// <summary>
/// The command that relays its functionality by invoking delegates.
/// </summary>
class RelayCommand : CommandBase
{
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute; /// <summary>
/// Initializes a new instance of the <see cref="RelayCommand"/> class.
/// </summary>
/// <param name="execute">The execute.</param>
/// <param name="canExecute">The can execute.</param>
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
if (canExecute == null)
{
// no can execute provided, then always executable
canExecute = (o) => true;
}
this._execute = execute;
this._canExecute = canExecute;
} /// <summary>
/// Defines the method that determines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
/// <returns>
/// true if this command can be executed; otherwise, false.
/// </returns>
public override bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute.Invoke(parameter);
} /// <summary>
/// Executes the command.
/// </summary>
/// <param name="parameter">The parameter.</param>
protected override void OnExecute(object parameter)
{
_execute?.Invoke(parameter);
}
}

​ 然后,我们实现一个示例,点击 Clear 按钮时,清空 TextBox 中的所有内容,那么需要声明一个 Model 和 ViewModel ,具体实现可以阅读 WPF 之 INotifyPropertyChanged 接口的使用 (一)

    class Student
{
public string Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
class MainWindowVM : NotifyProperty
{
private Student _student; public Student Student
{
get => _student;
set => SetProperty(ref _student, value);
}
public ICommand Clear { get; set; } private void ClearCommand(object args)
{
Student = new Student();
} public MainWindowVM()
{
_student = new Student()
{
Id = "01",
Name = "Dwayne",
Email = "974608610@qq.com",
Phone = "180888888888",
};
Clear=new RelayCommand(ClearCommand,null);
}
}

​ 最后,我们把该 ViewModel 绑定到 XAML 上,具体如下:

  <StackPanel Margin="20">
<Button Margin="10" Height="50" Content="Clear" Command="{Binding Path=Clear}"></Button>
<TextBox Text="{Binding Student.Id}" Height="50" VerticalContentAlignment="Center" Margin="10" BorderBrush="Black"></TextBox>
<TextBox Text="{Binding Student.Name}" Height="50" VerticalContentAlignment="Center" Margin="10" BorderBrush="Black"></TextBox>
<TextBox Text="{Binding Student.Email}" Height="50" VerticalContentAlignment="Center" Margin="10" BorderBrush="Black"></TextBox>
<TextBox Text="{Binding Student.Phone}" Height="50" VerticalContentAlignment="Center" Margin="10" BorderBrush="Black"></TextBox>
</StackPanel>

​ 当我们点击 Clear 按钮时,所有的 TextBox 就被清空了显示。其实,上述这个例子就是一个简单的 MVVM 示例,它让逻辑代码和 UI 完全分离。对于 Command 来说,当我们要执行 TextChanged 事件时,需要添加 “System.Windows.Interactivity” ,然后在 XAML 中添加如下引用:

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  <TextBox Text="改变内容即可删除" Height="50" VerticalContentAlignment="Center" Margin="10" BorderBrush="Black" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding Clear}" CommandParameter="{Binding Path=Student}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>

当我们改变该文本框的内容时,也可执行 Clear 命令。

WPF 之命令(七)的更多相关文章

  1. WPF之命令浅谈

    一.认识命令 1.1命令的特点 提到“命令”,我们应该想到命令的发出者,命令的接受者,命令的内容,准备工作,完成任务,回报工作...与事件中的发送者,接受者,消息,处理,处理,处理一一对应,如果是单纯 ...

  2. WPF C# 命令的运行机制

    1.概述 1.1 WPF C# 命令的本质 命令是 WPF 中的输入机制,它提供的输入处理比设备输入具有更高的语义级别. 例如,在许多应用程序中都能找到的“复制”.“剪切”和“粘贴”操作就是命令. W ...

  3. WPF 的命令的自动刷新时机——当你 CanExecute 会返回 true 但命令依旧不可用时可能是这些原因

    原文:WPF 的命令的自动刷新时机--当你 CanExecute 会返回 true 但命令依旧不可用时可能是这些原因 在 WPF 中,你可以使用 Command="{Binding Walt ...

  4. WPF Demo19 命令、UC

    命令系统的基本元素和关系WPF命令系统的组成要素:A.命令(command):WPF命令实际上就是实习了ICommand接口的类.平时使用最多的就是RoutedCommand类.B.命令源(comma ...

  5. WPF 跟踪命令和撤销命令(复原)

    WPF 命令模型缺少一个特性是复原命令.尽管提供了一个 ApplicationCommands.Undo 命令,但是该命令通常被用于编辑控件(如 TextBox 控件),以维护它们自己的 Undo 历 ...

  6. WPF 自定义命令 以及 命令的启用与禁用

    自定义命令:     在WPF中有5个命令类(ApplicationCommands.NavigationCommands.EditingCommands.ComponentCommands 以及 M ...

  7. WPF自定义命令

    WPF的自定义命令实现过程包括三个部分,定义命令.定义命令源.命令调用,代码实现如下: public partial class MainWindow : Window { public MainWi ...

  8. 按键(ESC ,F1,F2等)——wpf的命令处理方法

    WPF窗体的命令绑定   方法一:使用代码 <WpfUI:View.CommandBindings> <CommandBinding Command="Help" ...

  9. WPF——执行命令清空文本框

    一.造一个窗体,在窗体里面先造一个StackPanel,然后再StackPanel里面放好按钮和文本框,注意给所有的控件和容器起名字 <Grid> <StackPanel Name= ...

随机推荐

  1. g/test/s/lose/won/g

    包含字符串test的任意行商,用lose代替won

  2. OpenID协议

    背景 当我们要使用一个网站的功能时,一般都需要注册想用的账号.现在的互联网应用很多,一段时间之后你会发现你注册了一堆账号密码,根本记不住. 你可能会想到所有的网站都用同一套用户名和密码,这样虽然能解决 ...

  3. CMU数据库(15-445)Lab0-环境搭建

    0.写在前面 从这篇文章开始.开一个新坑,记录以下自己做cmu数据库实验的过程,同时会分析一下除了要求我们实现的代码之外的实验自带的一些代码.争取能够对实现一个数据库比较了解.也希望能写进简历.让自己 ...

  4. 创建Django REST framework工程

    1.创建工程虚拟环境 2.创建工程目录和调整目录结构: 创建Django的项目 创建docs 用于存放一些说明文档资料 创建scripts 用于存放管理脚本文件 创建logs 用于存在日志 在与项目同 ...

  5. MySQL简介及安装 mysql Ver 14.14 Distrib 5.7.28

    1.MySQL简介 1.数据库产品演变 第一代数据库架构: RDBMS 关系型数据库时代 : 合的时代 代表产品 :Oracle .MSSQL .MySQL.SQL server 第二代数据库架构:拆 ...

  6. docker容器的基本命令

      #安装docker yum -y install docker systemctl start docker.service systemctl status docker systemctl e ...

  7. IDEA_2019.1版本中Protobuf的使用

    一.Protobuf是什么 Protobuf 是 Google 发布的开源项目,全称 Google Protocol(/'prəʊtəkɒl/,协议,草案) Buffers,是一种轻便高效的结构化数据 ...

  8. 调度 GMP

    小结: 1. 当M从P的本地运行队列获取G时, 如果发现本地队列为空会尝试从其他P盗取一半的G过来,这个机制叫做Work Stealing, 2. Q M一定需要p吗? A 不一定.M正在执行原生代码 ...

  9. Python中单引号,双引号,三引号的区别

    Python中的字符串一般用单引号('A'),双引号("A")和三引号('''A''')或者("""A""") 1.单引 ...

  10. LOJ10066

    LOJ10066 新的开始 题目描述 发展采矿业当然首先得有矿井,小 F 花了上次探险获得的千分之一的财富请人在岛上挖了 n 口矿井,但他似乎忘记考虑的矿井供电问题-- 为了保证电力的供应,小 F 想 ...