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;
        }
    }

你可能感兴趣的:(c#,开发语言)