MVVM框架解析(一)

   花了一点时间看了一下微软开源MVVM代码,受义很多!

  从代码整体上看,代码分为四大类,  

从图中看不能明白我要表达的意思。显而意见!MainApplicationWindow.xaml是应用程序主窗口(V层),DocumentManager.cs是数据管理层,PersonViewModel.cs 是数据层(M层)。

DocumentManagerAdapter.cs可能有点难以明白,为什么DocumentManager.cs数据管理层,为什么还要Adapter层呢!其实这是分工更细化,DocumentManagerAdapter.cs是程序逻辑层(VM层)

从运行后窗口看,框架有两个tabItem多文档,从这里可以看出框架是多档,所以要DocumentManager.cs是数据管理层,将应用程序打开的所有文档进一步的管理,其实这层还是停留在M层。

 

整体了解了下,现在开始分析细节,四个类三层是怎样结合到一起的呢!

 

 1 namespace MvvmDemo {
 2 
 3     /// <summary>
 4     /// Contains the <c>Main()</c> method that starts the application
 5     /// </summary>
 6     public static class MainMvvmDemonstrationApplication {
 7 
 8         /// <summary>
 9         /// Starts the application
10         /// </summary>
11         /// <param name="args">The collection of program arguments.</param>
12         [STAThread]
13         public static int Main(string[] args) {
        ......
25 try { 26 #endif 27 ........30 31 // Initializes an instance of your System.Windows.Application derived class 32 var application = new MvvmDemo.View.App(); 33 34 // Instantiates the main window class 35 var mainWindow = new MvvmDemo.View.MainApplicationWindow(); 36 37 // Initializes the view-model layer with the view layer’s document manager adapter 38 // and tie it into the persistence layer 39 /**********************************************************************************/ 40 // 这两句很重要 41 // docContainer是主窗口tab控件 42 // adapter是tab的文档管理类 43 var docContainer = mainWindow.MasterDocumentContainer; 44 var adapter = new MvvmDemo.View.DocumentManagerAdapter(docContainer); 45 /**********************************************************************************/
46          .......

52 application.Run(mainWindow); 53            ........ 64 } 65 66 67 }//class 68 69 }//namespace

出点一些多途的代码,现在看条理就清楚多了。在Main定义了四个全局变量,

var application = new MvvmDemo.View.App();
 var mainWindow = new MvvmDemo.View.MainApplicationWindow();
 var docContainer = mainWindow.MasterDocumentContainer;
 var adapter = new MvvmDemo.View.DocumentManagerAdapter(docContainer);

这四个类型在前面已经说到了两个,对象
docContainer 是 mainWindow 对象中的变量,其实可以当作控件TabControl。对象application有一点编程经验的都知道,不明白可以到网上找找,我在这里不多讲了。

在前面不是说了还有一个DocumentManager.cs怎么在这里没有看到它呢,先别急,我先分析完对象 DocumentManagerAdapter这个对象里有一个引用mainWindow中的docContainer (相当于TabControl)
其实DocumentManagerAdapter里还有一个DocumentManager对象,因为它是mainWindow和DocumentManager之间的桥梁(相当于VM层)这样理解还不如说docContainer (相当于TabControl)和Document这间要桥梁。

很晚了,有时间在接着下吧!代码在后文本加上。

posted @ 2014-10-24 23:29  玖彩技术团队  阅读(842)  评论(0编辑  收藏  举报
以上只是个人想法和实践经验,如果有文字错误和语法错误,请加以指点! QQ:247039968 emil:wujc@younger.com 无论是美女的歌声,还是鬣狗的狂吠,无论是鳄鱼的眼泪,还是恶狼的嚎叫,都不会使我动摇