建立可扩展的silverlight应用框架 step-5:整理Module

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

这里记录下整理步骤

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

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

01 public class HelloPrismModel : INotifyPropertyChanged
02  {
03     public HelloPrismView view { get; private set; } 
04
05     public HelloPrismModel(HelloPrismView view)
06      {
07         this.view = view;
08         view.model  = this;
09     }
10
11     #region INotifyPropertyChanged  Members
12
13     public event PropertyChangedEventHandler  PropertyChanged;
14
15
16     private void OnPropertyChanged (string propertyName)
17     {
18          PropertyChangedEventHandler handler = this.PropertyChanged;
19          if (handler != null)
20         {
21              handler(this, new PropertyChangedEventArgs(propertyName));
22          }
23     }
24     #endregion
25
26 }

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

在接口中声明其模型属性

1 public interface IHelloPrismView
2 {
3      HelloPrismModel model { get; set; }
4 }

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

01 public partial class HelloPrismView : UserControl,  IHelloPrismView
02 {
03     private readonly IModuleManager  moduleManager;
04
05     public HelloPrismView()
06      {
07         InitializeComponent();
08     }
09      public HelloPrismView(IModuleManager moduleManager):this()
10      {
11         this.moduleManager = moduleManager;
12     } 
13     #region IHelloPrismView Members
14
15     public  HelloPrismModel model
16     {
17         get { return  this.DataContext as HelloPrismModel; }
18         set {  this.DataContext = value; }
19     }
20
21      #endregion
22 }4.最后重构一下HelloPrismModule类

01 public class HelloPrismModule : IModule
02 {
03      private readonly IRegionManager regionManager;
04     private  readonly IUnityContainer container;
05     private readonly  IModuleManager moduleManager;
06
07     public HelloPrismModule (IUnityContainer container, IRegionManager regionManager, IModuleManager  moduleManager)
08     {
09         this.container =  container;
10         this.regionManager = regionManager;
11          this.moduleManager = moduleManager;
12     }
13
14      public void Initialize()
15     {
16          HelloPrismModel helloPrismModel =  this.container.Resolve<HelloPrismModel>();
17
18          IRegion mainRegion = this.regionManager.Regions["MainRegion"];
19
20          object view = mainRegion.GetView("mainCurrView");
21          if (view != null)
22             mainRegion.Remove (view);
23
24         mainRegion.Add(helloPrismModel.view,  "mainCurrView");
25     }
26
27 }

附录

展示一下为Step-4开发的asp.net程序来管理我的xap文件。

管理界面:

生成的配置项:

posted @ 2011-09-07 14:02  Areas  阅读(252)  评论(0编辑  收藏  举报