深入浅出话命令(Command)-笔记(-)

一 基本概念

命令的基本元素:

  1. 命令(Command):实现了ICommand接口的类,平常使用最多的是RoutedCommand类。
  2. 命令源(Command Source):命令的发送者,实现了ICommandSource接口的类。
  3. 命令目标(Command Target):命令的执行者。,实现了IInputElement接口的类。
  4. 命令关联(Command Binding):把外围逻辑与命令关联起来

命令的使用步骤:

  1. 创建命令类:创建一个实现ICommand接口的类。
  2. 申明命令实例:实例一个命令类对象。某种操作只需实例化一个对象然后与之对应(单件模式)。
  3. 指定命令源:指定谁来发送命令。
  4. 指定命令目标:指定谁来执行命令。并不是命令属性,而是命令源属性。
  5. 设置命令关联:判断命令是否可执行,执行完成后采取的动作等。

二 小试牛刀

实现这样一个需求:定义一个命令,使用Button 来发送这个命令,当命令到达Textbox时,清空Text(当Textbox为空时,Button不可用)。

XAML代码:

<Window x:Class="CommandApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CommandApplication"
        mc:Ignorable="d"
        Title="MainWindow" Height="175" Width="200">
    <StackPanel x:Name="stackPanel">
        <Button x:Name="button1" Content="Send Command" Margin="5"/>
        <TextBox x:Name="textBoxA" Margin="5,0" Height="100"/>
    </StackPanel>
</Window>

CS代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CommandApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitializeCommand();
        }
        //RoutedCommand 是系统自带常用的命令类 
        //申明命令实例
        private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof(MainWindow));
        private void InitializeCommand()
        {
            //指定命令源
            this.button1.Command = this.clearCmd;
            this.clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));
            //指定命令目标,命令源属性中指定s
            this.button1.CommandTarget = this.textBoxA;

            //创建命令关联
            CommandBinding cb = new CommandBinding();
            cb.Command = this.clearCmd;
            cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
            cb.Executed += new ExecutedRoutedEventHandler(cb_Execute);

            //this.stackPanel.CommandBindings.Add(cb);
            this.textBoxA.CommandBindings.Add(cb);
        }

        //当命令送达目标后,此方法被调用
        private void cb_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            this.textBoxA.Clear();
            e.Handled = true;

        }
        //当探测命令是否可执行时,此方法被调用
        private void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBoxA.Text))
                { e.CanExecute = false; }
            else
            { e.CanExecute = true; }
            e.Handled = true;

        }
    }
}

运行效果:

 posted on 2018-03-28 11:21  龍龍520  阅读(450)  评论(0编辑  收藏  举报