码迷,mamicode.com
首页 > 其他好文 > 详细

MVVM框架解析(一)

时间:2014-10-25 00:44:44      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   sp   数据   div   

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

  从代码整体上看,代码分为四大类,bubuko.com,布布扣  

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

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

bubuko.com,布布扣

从运行后窗口看,框架有两个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这间要桥梁。

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

MVVM框架解析(一)

标签:style   blog   http   color   io   ar   sp   数据   div   

原文地址:http://www.cnblogs.com/qq247039968/p/4049533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!