• WPF之MVVM(Step3)——使用Prism(1)


    使用WPF-MVVM开发时,自己实现通知接口、DelegateCommand相对来说还是用的较少,我们更多的是使用第三方的MVVM框架,其中微软自身团队提供的就有Prism框架,此框架功能较多,本人现学有限,暂时先介绍简单的使用。

    注:此处使用Prism版本4.1

    Prism首先方便我们的是帮助我们实现了INotifyPropertyChanged接口,其中RaisePropertyChanged不仅实现利用字符串通知,更实现了Lambda表达式,这样在代码重构上提供了一些方便。。。

    其次,Prism帮助我们实现了ICommand接口,可以利用其DelegateCommand实现。

    下面为简要代码:

     class TestViewModel : NotificationObject
        {
            private string teststr;
            /// <summary>
            /// 待通知字符串
            /// </summary>
            public string TestStr
            {
                get { return teststr; }
                set
                {
                    teststr = value;
                    RaisePropertyChanged(()=>TestStr);
                }
            }
    
            /// <summary>
            /// 测试命令
            /// </summary>
            public ICommand TestCommand { get; set; }
    
    
            public TestViewModel()
            {
                TestCommand = new DelegateCommand(Test,CanTest);
            }
    
            int i = 0;
            /// <summary>
            /// testcommand执行的方法
            /// </summary>
            private void Test()
            {
                i++;
                TestStr = i.ToString();
            }
            /// <summary>
            /// testcommand是否可用
            /// </summary>
            /// <returns></returns>
            private bool CanTest()
            {
                return true;
            }
        }

    此处使用了Prism中的DelegateCommand,此类用于无需传递参数使用,下篇将描述使用DelegateCommand<T>创建可以传递参数的Command。


    项目代码托管地址:https://wpfmvvm.codeplex.com/

    就是爱玩耍,各种玩![‘假’技术‘真’宅]
    个人网站:http://www.shisujie.com
  • 相关阅读:
    501. Find Mode in Binary Search Tree
    [leetcode]Evaluate Reverse Polish Notation
    [leetcode]LRU Cache
    [mock]12月27日
    [mock]12月28日
    [leetcode]Max Points on a Line
    *[topcoder]JumpFurther
    [mock]12月11日
    *[topcoder]GUMIAndSongsDiv1
    [leetcode]Reorder List
  • 原文地址:https://www.cnblogs.com/huaxia283611/p/4202503.html
Copyright © 2020-2023  润新知