您的位置:首页 > 移动开发

About the application of lambda and relayCommand

2017-09-04 09:14 513 查看
 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才会执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐