• wpf学习笔记 Command


    Command是事件的进化体,Wpf中可以将Command绑定到控件上,减少重复的事件代码。它的方便在于可以直接在ui进行操作,并在逻辑中使命令不可用。

    微软为wpf内置了不少默认命令,诸如paste、copy、save等。下面的代码演示了一个简单的记事本程序。

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Menu Grid.Column="0">
                <MenuItem Command="Save" x:Name="menuSave" Header="Save"></MenuItem>
            </Menu>
            <TextBox x:Name="tbx" Grid.Row="1"></TextBox>
        </Grid>
        <Window.CommandBindings>
            <CommandBinding Command="Save" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" ></CommandBinding> 

    </Window.CommandBindings>

     在后台,实现了CanExecute和Executed的方法。

       private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                
                if ( tbx.Text != "")
                {
                    e.CanExecute = true;
                }
                else
                {
                    e.CanExecute = false;
                }
            }

            private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                 // 保存文件对话框
           SaveFileDialog save = new SaveFileDialog();
           save.Filter = "文本文件|*.txt|所有文件|*.*";
        
           bool? result = save.ShowDialog();
           if (result.Value)
           {
               // 执行保存文件操作
           }

    但是内置的毕竟比较少,我们可以继承ICommand接口来自定义Command,在ICmmand中,包含CanExecute和Executed成员,我们在DelegateCommand类中实现他们。

    public class DelegateCommand<T> : ICommand
        {
            private readonly Action<T> executeAction;
            private readonly Func<T, bool> canExecuteAction;
            public event EventHandler CanExecuteChanged;
            public DelegateCommand(Action<T> executeAction)
                : this(executeAction, null)
            {
            }
            public DelegateCommand(Action<T> executeAction,
                Func<T, bool> canExecuteAction)
            {
                this.executeAction = executeAction;
                this.canExecuteAction = canExecuteAction;
            }
            public bool CanExecute(object parameter)
            {
                return canExecuteAction==null ? true:canExecuteAction((T)parameter);
            }
            public void Execute(object parameter)
            {
                if (CanExecute(parameter))
                {
                    executeAction((T)parameter);
                }
            }

      我们可以很方便地将command绑定到ui元素上,实现ui与逻辑的分离。

    源码下载:/Files/shen6041/Command.zip 

     
  • 相关阅读:
    vue中 $event 的用法--获取当前父元素,子元素,兄弟元素
    vue的通信方式(二)---祖父孙三个级别的之间的隔代通信
    js获取当前页面url信息
    解决Vue刷新一瞬间出现样式未加载完或者出现Vue代码问题
    Java POI读取excel中数值精度损失
    Java读取Excel数值内容带.0或变科学计数法的解决办法
    Java专业术语
    jdbc连接数据库
    阿里云linux配置ftp服务
    mybatis批量更新表setting parameters 错误
  • 原文地址:https://www.cnblogs.com/shen6041/p/2380973.html
Copyright © 2020-2023  润新知