c#键盘事件的使用

c#键盘事件的使用

1.在xaml写入键盘绑定事件

 <KeyBinding Command="{Binding EnterInput1CMD}" Key="Enter" CommandParameter="{Binding ElementName=sendDataTextBox,Path=Text}" />

2.vm.cs写入对应的事件函数中

EnterInput1CMD = new RelayCommand((p) =>
            //键盘事件
            {
   
                System.Console.WriteLine(p);
                SetFocus1btnFlag = true;
                //if (tb1Str != "")
                {
   
                    byte[] bytes2send = System.Text.Encoding.Default.GetBytes((String)tb1Str + appendContent);
                    GlobeData.pommL2Obj.pcomDic[SettingWinVM.rs232port1Num].writePort(bytes2send);
                }
                tb1Str = "";

                SetFocus1tbFlag = true;

            });
 public RelayCommand(Action<object> execute)       //定义Action,CanExecute
            : this(execute, DefaultCanExecute)
        {
   
        }

3.在MvvmLight中实现ICommand接口的类是RelayCommand,RelayCommand通过构造函数初始化Execute 和 CanExecute方法,因此,构造函数传入的是委托类型的参数,Execute 和 CanExecute则执行的是委托的方法。
RelayCommand类

           public class RelayCommand : ICommand
    {
   
        private Action<object> execute;                     //
                                                            //
        private Action<object, object> execute2 {
    get; set; }                   //定义成员

        private Predicate<object> canExecute;//Predicate:述语//定义成员

        private event EventHandler CanExecuteChangedInternal;//事件

        public RelayCommand(Action<object> execute)       //定义Action,CanExecute
            : this(execute, DefaultCanExecute)
        {
   
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)//定义
        {
   
            if (execute == null)
            {
   
                throw new ArgumentNullException("execute");
            }

            if (canExecute == null)
            {
   
                throw new ArgumentNullException("canExecute");
            }

            this.execute = execute;
            this.canExecute = canExecute;
        }

        public event EventHandler CanExecuteChanged        //CanExecuteChanged事件处理方法
        {
   
            add
            {
   
                CommandManager.RequerySuggested += value;
                this.CanExecuteChangedInternal += value;
            }

            remove
            {
   
                CommandManager.RequerySuggested -= value;
                this.CanExecuteChangedInternal -= value;
            }
        }

        public bool CanExecute(object parameter)            //CanExecute方法
        {
   
            return this.canExecute != null && this.canExecute(parameter);
        }

        public void Execute(object parameter)              //Execute方法
        {
   
            this.execute(parameter);
        }

        public void OnCanExecuteChanged()                //OnCanExecute方法
        {
   
            EventHandler handler = this.CanExecuteChangedInternal;
            if (handler != null)
            {
   
                //DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty));
                handler.Invoke(this, EventArgs.Empty);
            }
        }

        public void Destroy()                          //销毁方法
        {
   
            this.canExecute = _ => false;
            this.execute = _ => {
    return; };
        }

        private static bool DefaultCanExecute(object parameter)  //DefaultCanExecute方法
        {
   
            return true;
        }
    }

相关推荐

  1. c#键盘事件使用

    2024-01-20 14:48:01       30 阅读
  2. WPF 键盘事件捕获

    2024-01-20 14:48:01       26 阅读
  3. Qt | 键盘事件

    2024-01-20 14:48:01       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-20 14:48:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-20 14:48:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-20 14:48:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-20 14:48:01       20 阅读

热门阅读

  1. iptables TEE模块测试小记

    2024-01-20 14:48:01       35 阅读
  2. GoLang刷题之leetcode

    2024-01-20 14:48:01       33 阅读
  3. Vue学习1

    2024-01-20 14:48:01       30 阅读
  4. 【加速排坑】docker设置国内image镜像源

    2024-01-20 14:48:01       38 阅读
  5. springboot 集成websocket

    2024-01-20 14:48:01       34 阅读
  6. 记录 | 修改.gitignore文件,如何重新生效

    2024-01-20 14:48:01       36 阅读
  7. 深度解析window.history.go()和history.back()

    2024-01-20 14:48:01       33 阅读
  8. windows 利用DDNS-GO解析IPV6

    2024-01-20 14:48:01       42 阅读
  9. Todo List 变成 Contribution List

    2024-01-20 14:48:01       31 阅读
  10. C++:史上最坑小游戏

    2024-01-20 14:48:01       32 阅读
  11. Unity音频管理器

    2024-01-20 14:48:01       31 阅读
  12. QML与C++交互详解

    2024-01-20 14:48:01       34 阅读