命令

对命令理解不是太深刻,记录下:

系统命令:ApplicationCommands、NavigationCommands、EditingCommands、ComponentCommands、MediaCommands等

以ApplicationCommands.New举例说明,ApplicationCommands.New是RoutedUICommand类型,RoutedUICommand类型比RoutedCommand多个个text属性而已;

命令源:即发生命令的元素,如button、CheckBox、单独的ListBoxItem、HyperLink、MenuItem等,它们都是实现了ICommandSource接口

namespace System.Windows.Input
{
    //
    // 摘要:
    //     定义了解如何调用命令的对象。
    public interface ICommandSource
    {
        //
        // 摘要:
        //     获取将在调用命令源时执行的命令。
        //
        // 返回结果:
        //     将在调用命令源时执行的命令。
        ICommand Command { get; }
        //
        // 摘要:
        //     表示可在执行命令时传递给该命令的用户定义的数据值。
        //
        // 返回结果:
        //     命令特定数据。
        object CommandParameter { get; }
        //
        // 摘要:
        //     将在其上执行命令的对象。
        //
        // 返回结果:
        //     将在其上执行命令的对象。
        IInputElement CommandTarget { get; }
    }
Command 、CommandParameter没什么好说的,分别是要执行的命令对象、附带的参数数据,
CommandTarget是目标对象,如点击按钮时清空textbox,textbox就是CommandTarget,如果没有为命令源指定命令目标,拥有焦点就是命令目标;

命令绑定:命令绑定就像一座桥梁一样,联通了命令源与目标的联系,以及限制命令的状态是否可用,如果没有“桥梁”命令会被禁用;

 

 

写一个例子:
<Window x:Class="WpfApp1Command.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:WpfApp1Command"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid Margin="0,231,0,99">
        <StackPanel>
            <Button Width="80" Height="30" Content="click me"   Command="ApplicationCommands.New"   ></Button>
            <TextBox x:Name="text1" ></TextBox>
        </StackPanel>
    </Grid>
</Window>
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 WpfApp1Command
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            CommandBinding bding = new CommandBinding(ApplicationCommands.New);
            bding.Executed += Bding_Executed;
            bding.CanExecute += Bding_CanExecute;
            this.CommandBindings.Add(bding);
        }
        //当探测命令是否可以执行时,触发此事件方法
        private void Bding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (text1.Text == "")
            {
                e.CanExecute = true;
            }
            else
            {
                e.CanExecute = false;
            }
          
            //避免向上继续传播降程序低性能
            e.Handled = true;
        }

        private void Bding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("命令源是:");
            sb.Append(e.Source.ToString());
            sb.Append(",命令对象是:");
            sb.Append((e.Command as RoutedUICommand).Text);
            sb.Append(",命令目标是:");
            sb.Append((e.Source as Button).CommandTarget);
            text1.Text = sb.ToString();
            //避免向上继续传播降程序低性能
            e.Handled = true;
        }

        private void cmdDoCommand_Click(object sender, RoutedEventArgs e)
        {
            // 直接调用命令的两种方式
            ApplicationCommands.New.Execute(null, (Button)sender);
        }
    }
}

有2点疑问:命令源没有指定CommandTarget时,谁是命令目标呢?private void Bding_Executed(object sender, ExecutedRoutedEventArgs e)中的e.Source为什么在指定了CommandTarget时就发生了变化呢?请高手回答下



posted @ 2020-09-23 13:46  _MrZhu  阅读(205)  评论(0)    收藏  举报