专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WPF

WPF之MVVM(Step3)——使用Prism(一)

发布时间:2011-06-23 13:56:08 文章来源:www.iduyao.cn 采编人员:星星草
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/

1楼angtianqiang
prism过于复要,楼主可以看看其它的MVVM框架,比如MVVMLIGHT
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: