About the application of lambda and relayCommand

来源:互联网 发布:java字符串转换整形 编辑:程序博客网 时间:2024/06/04 18:48
 public RelayCommand<TextEditor> SaveCommand
        {
            get
            {
?? 代表这个返回的_saveCommand为false时,就new一个新对象返回。
                return _saveCommand
                    ?? (_saveCommand = new RelayCommand<TextEditor>(
                    editor =>
                    {
                        try
                        {
                            // Can't add Encoding when save, it's wired, but it's the fact. Otherwise some wired character will appear at the beginning of line 1
                            File.WriteAllText(Case.CasePath, editor.Text);
                        }
                        catch (System.Exception e)
                        {
                            MessageBox.Show(e.Message);
                            return;
                        }
                        MessageBox.Show("Case " + Case.CaseName + " saved.");
                    }, (x) => !this.CaseIsRunning));
            }
        }
editor => 和(x) =>这两个lambda函数作为参数在RelayCommand的构造函数中。
如_saveCommand=new RelayCommand<TextEditor>(void A{},bool B{});当B为真时,A函数才执行。
这种语法是在RelayCommand的构造函数中定义的,A是excute()方法,后面是CanExcute()后者返回一个bool类型,当返回值为ture是excute才会执行。
阅读全文
0 0