zoukankan      html  css  js  c++  java
  • 命令

    基础点

    元素:

      命令: 平时最多使用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" />

  • 相关阅读:
    日志规范实践
    序列化和反序列化及Protobuf 基本使用
    简述TCP网络编程本质
    笔记:多线程服务器的适用场合(1)
    聊聊同步、异步、阻塞与非阻塞(转)
    《EntrePreneur》发刊词
    make和makefile简明基础
    luogu P3687 [ZJOI2017]仙人掌 |树形dp
    luogu P3172 [CQOI2015]选数 |容斥原理
    luogu P4513 小白逛公园 |线段树
  • 原文地址:https://www.cnblogs.com/evemen/p/6239328.html
Copyright © 2011-2022 走看看