MVVM和MVVMLight框架介绍及在项目中的使用详解

上一篇我们已经介绍了如何使用NuGet把MVVMLight应用到我们的WPF项目中。这篇我们来了解下一个基本的MVVMLight框架所必须的结构和运行模式。

MVVMLight安装之后,我们可以看到简易的框架布局,如上篇,生成了一个ViewModel文件夹,ViewModel层的内容都放在这边,除了Main对象的ViewModel之外,还包含一个ViewModelLocator文件,

用来注入当前的ViewModel全局实例。

一、先来说说分层结构:

如图:

1、View负责前端展示,与ViewModel进行数据和命令的交互。

2、ViewModel,负责前端视图业务级别的逻辑结构组织,并将其反馈给前端。

3、Model,主要负责数据实体的结构处理,与ViewModel进行交互。

根据上述的分层,我们来进行编码。

先建立一个完整三层结构的目录,如图,包含Model、View、ViewModel三层文件夹:

1、写一个Model,代码如下:

 using GalaSoft.MvvmLight;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace MVVMLightDemo.Model
 {
     public class WelcomeModel : ObservableObject
     {
         private String introduction;
         /// <summary>
         /// 欢迎词
         /// </summary>
         public String Introduction
         {
             get { return introduction; }
             set { introduction = value; RaisePropertyChanged(()=>Introduction); }
         }
     }
 

很简单,仅仅是包含一个实体对象,这边注意的的是那他继承了一个父类:ObservableObject,这个父类的作用就是保证能够检测属性是否被改变。

它实现了INotifyPropertyChanged接口,通过触发PropertyChanged事件达到通知UI更改的目的;

所以我们在定义实体对象的时候,只需要调用RaisePropertyChanged(PropertyName)就可以进行属性更改通知了。

所以实体里面定义的每个属性都加上RaisePropertyChanged(PropertyName)的调用,就可以实现对UI的交互更新了。

2、写一个VideModel,来负责跟View的交互。

using GalaSoft.MvvmLight;
using MVVMLightDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMLightDemo.ViewModel
{
    public class WelcomeViewModel:ViewModelBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public WelcomeViewModel()
        {
            Welcome = new WelcomeModel() { Introduction = "Hello World!" };
        }
        #region 属性

        private WelcomeModel welcome;
        /// <summary>
        /// 欢迎词属性
        /// </summary>
        public WelcomeModel Welcome
        {
            get { return welcome; }
            set { welcome = value; RaisePropertyChanged(()=>Welcome); }
        }
        #endregion
    }
}

也很简单,包含了一个命名为Welcome的WelcomeModel属性,继承了ViewBaseModel父类,

ViewBaseModel同时继承 ObservableObject类和ICleanup接口。所以他同样有INotifyPropertyChanged接口的能力,

能够通过触发PropertyChanged事件达到通知View的目的;

构造函数中对 Welcome 属性进行了实例化。

3、写一个View,来显示和交互ViewModel。

<Window x:Class="MVVMLightDemo.View.WelcomeView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Title="WelcomeView" Height="300" Width="300">
     <Grid>
         <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" >
             <TextBlock Text="{Binding Welcome.Introduction}" FontSize="30" ></TextBlock>         
         </StackPanel>        
     </Grid>
 </Window>

 TextBlock 绑定了 Welcome.Introduction,所以应该显示Welcome对象下的Introduction属性。

这时候的ViewModel和View是没有任何关系的,所以我们在code-Behind的构造函数中写上如下代码: 

using MVVMLightDemo.ViewModel;
using System.Windows;
 namespace MVVMLightDemo.View
 {
     /// <summary>
     /// Interaction logic for WelcomeView.xaml
     /// </summary>
     public partial class WelcomeView : Window
     {
         public WelcomeView()
         {
             InitializeComponent();
             this.DataContext = new WelcomeViewModel();
         }
     }

把 WelcomeViewModel 赋值给当前视图的数据上下文。所以可以在当前视图中使用ViewModel中所有的公开属性和命令。

执行效果如下:

二、再来说说构造器:

如果使用NuGet安装的是完整的一个是MVVM Light 框架,而非 MVVM Light libraries only的时候,总是会带上ViewModelLocator类,并且生成资源字典并加入到了全局资源中。

<Application x:Class="MVVMLightDemo.App" 
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
              StartupUri="View/WelcomeView.xaml" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              d1p1:Ignorable="d" 
              xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:vm="clr-namespace:MVVMLightDemo.ViewModel" >
   <Application.Resources>
     <ResourceDictionary>
             <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
     </ResourceDictionary>
   </Application.Resources>
 </Application>

所以每次App初始化的时候,就会去初始化ViewModelLocator类。

实际上他就是一个很基本的视图模型注入器。在构造器中把使用到的ViewModel统一注册,并生成单一实例。

然后使用属性把它暴露出来,每当我们访问属性的时候,就会返回相应的ViewModel实例。

/*
   In App.xaml:
   <Application.Resources>
       <vm:ViewModelLocator xmlns:vm="clr-namespace:MVVMLightDemo"
                            x:Key="Locator" />
   </Application.Resources>
   
   In the View:
   DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
 
   You can also use Blend to do all this with the tool's support.
   See http://www.galasoft.ch/mvvm
 */
 using GalaSoft.MvvmLight;
 using GalaSoft.MvvmLight.Ioc;
 using Microsoft.Practices.ServiceLocation;
 namespace MVVMLightDemo.ViewModel
 {
     /// <summary>
     /// This class contains static references to all the view models in the
     /// application and provides an entry point for the bindings.
     /// </summary>
     public class ViewModelLocator
     {
         /// <summary>
         /// Initializes a new instance of the ViewModelLocator class.
         /// </summary>
         public ViewModelLocator()
         {
             ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
             #region Code Example
             ////if (ViewModelBase.IsInDesignModeStatic)
             ////{
             ////    // Create design time view services and models
             ////    SimpleIoc.Default.Register<IDataService, DesignDataService>();
             ////}
             ////else
             ////{
             ////    // Create run time view services and models
             ////    SimpleIoc.Default.Register<IDataService, DataService>();
             ////}
             #endregion
             SimpleIoc.Default.Register<MainViewModel>();          
         }
         #region 实例化
         public MainViewModel Main
         {
             get
             {
                 return ServiceLocator.Current.GetInstance<MainViewModel>();
             }
         }
         #endregion
         public static void Cleanup()
         {
             // TODO Clear the ViewModels
         }
     }
 

注意的是,这边把MVVMLight 自带的SimpleIoc作为默认的服务提供者,它是个简易的注入框架。

为了统一化,并且在设计的时候可以看到看到ViewModel的数据,这边用ServiceLocator 又将SimpleIoc包裹了一层。

上面我们写了一个Hello World,这时候就可以用这种方式改装了。

/*
   In App.xaml:
   <Application.Resources>
       <vm:ViewModelLocator xmlns:vm="clr-namespace:MVVMLightDemo"
                            x:Key="Locator" />
   </Application.Resources>
   In the View:
   DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
 
   You can also use Blend to do all this with the tool's support.
   See http://www.galasoft.ch/mvvm
 */
 using GalaSoft.MvvmLight;
 using GalaSoft.MvvmLight.Ioc;
 using Microsoft.Practices.ServiceLocation;
 namespace MVVMLightDemo.ViewModel
 {
     /// <summary>
     /// This class contains static references to all the view models in the
     /// application and provides an entry point for the bindings.
     /// </summary>
     public class ViewModelLocator
     {
         /// <summary>
         /// Initializes a new instance of the ViewModelLocator class.
         /// </summary>
         public ViewModelLocator()
         {
             ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
             #region Code Example
             ////if (ViewModelBase.IsInDesignModeStatic)
             ////{
             ////    // Create design time view services and models
             ////    SimpleIoc.Default.Register<IDataService, DesignDataService>();
             ////}
             ////else
             ////{
             ////    // Create run time view services and models
             ////    SimpleIoc.Default.Register<IDataService, DataService>();
             ////}
             #endregion
             SimpleIoc.Default.Register<MainViewModel>();
             SimpleIoc.Default.Register<WelcomeViewModel>();
         }
         #region 实例化
         public MainViewModel Main
         {
             get
             {
                 return ServiceLocator.Current.GetInstance<MainViewModel>();
             }
         }
         public WelcomeViewModel Welcome
         {
             get
             { 
                return ServiceLocator.Current.GetInstance<WelcomeViewModel>();
             }
         }
         #endregion
         public static void Cleanup()
         {
             // TODO Clear the ViewModels
         }
     }
 

注册完WelcomeViewModel实例之后,我们就可以在相应的View中使用了 ,原本的

 public WelcomeView()
 {
         InitializeComponent();
         this.DataContext = new WelcomeViewModel();
 }

中的 this.DataContext = new WelcomeViewModel();

可以去掉了,直接在WelcomeView中这样写:

DataContext="{Binding Source={StaticResource Locator},Path=Welcome}"

如下图:

这样做的好处,一个是绑定化相对于简单粗暴的赋值方式,更合理。一个是在可视化窗口可以看到所绑定的数据,达到所见即所得的友好效果。

如下:

当我们改掉绑定到的数据,编译之后就会立马呈现:

服务端开发人员可以专心写ViewModel的业务逻辑代码,UI开发人员可以专注设计视图了,

同样 ViewModel可以绑定到不同的视图上,所以从这边就可以体现出他其中的三个重要特性:低耦合、可重用性、独立开发。

大家有没有发现ViewModelLocator 类中还有个 ClearnUp()方法,主要目的用来清除ViewModel实例的。

ViewModelBase继承了GalaSoft.MvvmLight.ICleanup接口,并在自己的类中写好了Cleanup()虚方法。所以我们在实例ViewModel类中可以重写Cleanup()来达到清除当前实例的目的。

以上就是MVVMLight 之Model View结构及全局视图模型注入器的详细内容,更多关于ViewModel 结构及全局视图模型注入器的资料请关注Devmax其它相关文章!

MVVMLight项目Model View结构及全局视图模型注入器的更多相关文章

  1. ios – 如何在父视图上添加子视图控制器的视图

    再现.从而:

  2. ios – 实例化ViewController w / xib时的帧大小不正确

    我有一个视图控制器,看起来像:当我通过让spotViewController=SpotViewController实例化视图控制器并将其推送到导航控制器时,结果框架在viewDidLoad和viewWillAppear中都是不正确的.它给了我这是界面构建器中的大小.为什么会发生这种情况以及使用xib实例化视图控制器以确保帧正确的正确方法是什么?解决方法这将解决您的问题,UIViewController从xib加载,在viewDidLoad期间保持xib大小:

  3. ios – CAGradientLayer不工作

    参见英文答案>DrawinggradientonUIViewnotworkingwithiOS91个我创建了一个新项目.我链接了QuartzCore.framework并导入了在ViewController.m中.这是代码.我尝试将视图的背景颜色设置为clearColor,在viewDidAppear中调用它,但它们都没有工作.我真的不知道自己错过

  4. Swift 简单控件示例:活动指示器UIActivityIndicatorView

    转载请声明出处:http://www.jb51.cc/article/p-fycefhzj-ga.html---------------------------------------------------------------------------------------------------------------------------------------------------

  5. Swift 文本编辑时将文本框上移

  6. UIScrollView加载子视图偏移64的问题

    最近正在学swift,基本语法刚刚看完,试着实践一下。没想到用scrollView的时候遇到问题了。o(╯□╰)o在一个VC里如果第一个控件是UIScrollView,注意是第一个控件,就是首先addsubview在VC.view上。接着加到scrollView上的View就会在Y点上发生64的偏移。这个在iOS7以后才会出现。如果这个scrollView不是第一个加到self.view上的。

  7. AutoLayout Swift demo

    importUIKitimportSwiftclassViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()letv1=UIView()letv2=UIView()v1.backgroundColor=UIColor.redColor()v2.backgroundColor=UIColor.blue

  8. Swift UIView 常用添加方法

    Swift添加UIView有几种常见方法首先addSubview最常见就是普通的添加我们看下效果解析来我们获取一下self.view的子视图,然后就知道刚才添加的两个视图的index结果为4,那么view1index为2,view2的index为3下来我们看下这个方法insertSubview(view:UIView,atIndexindex:Int)将view添加上来效果如下我们可以看到blue

  9. Swift UIView 层次调整

    Swift中添加的UIView都是有层级的。我们先添加三个看一看效果如下我们可以看到他们的层次是按照添加顺序走的那么我们尝试调整一下他的层次我们尝试一下以上三个方法就明白他们的意思了苹果开发群:414319235欢迎加入欢迎讨论问题

  10. Swift中View层级结构调整

    //父控件中的某个子控件显示在图层最上view.bringSubviewToFront();

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部