前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >建立可扩展的silverlight 应用框架 step-5

建立可扩展的silverlight 应用框架 step-5

作者头像
用户1172164
发布2018-01-16 11:42:59
4560
发布2018-01-16 11:42:59
举报

整理Module

在第三节里边,建立了一个最简单的Module。这里要对其再进行整理。之前我写过一篇《简练的视图模型 ViewModel》这里就讲述了一个最最基本的运用视图与模型的例子。用模型来控制视图的呈现在很早的时候就提出来了。当然Prism这个框架也包括了这一点。这里就要为Module加入Model。 这里记录下整理步骤

1.在Module项目中新建立一个Models文件夹,用来存放数据模型。在文件下新建立一个HelloPrismModel类继承自INotifyPropertyChanged。

在构造函数中将视图作为参数传入将视图和模型相绑定。

代码语言:javascript
复制
    public class HelloPrismModel : INotifyPropertyChanged
    {
        public HelloPrismView view { get; private set; }

        public HelloPrismModel(HelloPrismView view)
        {
            this.view = view;
            view.model = this;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }

2.在Views文件下新建立一个IHelloPrismView接口。

在接口中声明其模型属性

代码语言:javascript
复制
    public interface IHelloPrismView
    {
        HelloPrismModel model { get; set; }
    }

3.修改原来的HelloPrismView.xaml继承自IHelloPrismView接口并且实现接口。

代码语言:javascript
复制
    public partial class HelloPrismView : UserControl, IHelloPrismView
    {
        private readonly IModuleManager moduleManager;

        public HelloPrismView()
        {
            InitializeComponent();
        }
        public HelloPrismView(IModuleManager moduleManager):this()
        {
            this.moduleManager = moduleManager;
        }
        #region IHelloPrismView Members

        public HelloPrismModel model
        {
            get { return this.DataContext as HelloPrismModel; }
            set { this.DataContext = value; }
        }

        #endregion
    }

4.最后重构一下HelloPrismModule类

代码语言:javascript
复制
    public class HelloPrismModule : IModule
    {
        private readonly IRegionManager regionManager;
        private readonly IUnityContainer container;
        private readonly IModuleManager moduleManager;

        public HelloPrismModule(IUnityContainer container, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;
        }

        public void Initialize()
        {
            HelloPrismModel helloPrismModel = this.container.Resolve<HelloPrismModel>();

            IRegion mainRegion = this.regionManager.Regions["MainRegion"];

            object view = mainRegion.GetView("mainCurrView");
            if (view != null)
                mainRegion.Remove(view);

            mainRegion.Add(helloPrismModel.view, "mainCurrView");
        }

    }  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-02-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档