zoukankan      html  css  js  c++  java
  • MVVM Light框架点滴(MVVM模式)

    MVVM Light框架是针对WPF和silverlight开发提供的一个MVVM模式的实现,以下简要总结一下该框架。

    [以下基于Silverlights4]

    MVVM Light组成

    目前的框架就两个库文件

    GalaSoft.MvvmLight库

    ViewModelBase:View Model的基础类,MVVM中VM实现

    Messenger:用于ViewModel和View之间传递的消息,注意系统的GalaSoft.MvvmLight.Messaging命名空间下已经预定义了一些常使用的消息处理类,
    如DialogMessage、NotificationMessageAction、NotificationMessageWithCallback等

    Command:命令,RelayCommand,用语和界面元素的响应处理绑定

    GalaSoft.MvvmLight.Extras库

    EventToCommand:使用XAML 绑定Command,Expression4中使用

    DispatcherHelper:处理多线程

    MVVM Light安装和使用

    GalaSoft.MvvmLight.Binaries.V3.zip

    解压到系统的Program files目录

    GalaSoft.MvvmLight.Templates.V3.VS10.zip

    将其中的ItemTemplates ProjectTemplates下的Silverlight内容拷贝到对应的

    C:\Users\Administrator\Documents\Visual Studio 2010\Templates下

    此时New Project是可以选择Silverlight下的MVVM light模板,不过这个模板也是比较简单的

    一般项目使用方法:

    1、 添加GalaSoft.MvvmLight.SL4.dll GalaSoft.MvvmLight.Extras.SL4.dll引用

    2、 在App.xaml中加入ViewModelLocator, 当然如何使用代码或则MEF实现View和ViewModel之间的绑定,这个也不需要

    xmlns:vm="clr-namespace:MvvmLight1.ViewModel"

    <vm:ViewModelLocator x:Key="Locator d:IsDataSource="True" />

    3、 添加ViewModel、View

    此时:

    ViewModel中:加入Command实现、发送消息和View 传递信息

    View中:       绑定数据、绑定Command、注册消息、ICleanup实现ViewModel清理注册消息清理、界面和交互设计
     
    至于View和ViewModel之间的初始化关联可以使用ViewModelLocator.cs的形式建立和绑定【这个形式还是有些麻烦】,当然也可以使用MEF等IOC实现自动注入

    View中绑定ViewModel可以使用

    DataContext="{Binding Main, Source={StaticResource Locator}}",也可以使用代码实现绑定[此处的Locator就是在App.xaml声明的]

    MVVM Light实例

    消息注册

    一般在View中注册以响应ViewModel中消息,当然也可以在别处注册

     Messenger.Default.Register(this, OnReadOnlyIssueMessage);
    Messenger.Default.Register<Brush>(this, true, m => BackgroundBrush = m);

    消息发送

    Messenger.Default.Send(new CommandMessage(Notifications.NotifyShutdown));
    Messenger.Default.Send<Brush, MainViewModel>(savedSettings.ApplicationBackgroundBrush);

    Command定义

            public RelayCommand ShutdownCommand
            {
                get;
                private set;
            }
      public MainViewModel()
            {
                if (IsInDesignMode)
                {
                   
                }
                else
                {
                   
                    ShutdownCommand = new RelayCommand(ShutdownService.RequestShutdown);
                }
            }
    RemoveFileCommand = new RelayCommand<Data.Web.File>(g => this.OnRemoveFileCommand(g), g => g != null);

    Command绑定

       <Button  Content="Shut" Command="{Binding Path= ShutdownCommand }" />
     
           Command="{Binding Path=RemoveFileCommand}"
           CommandParameter="{Binding SelectedItem, ElementName=listBox_Files}" 
     

    ICLeanup

    MEF

       public partial class AllIssues : UserControl, ICleanup
        {
            public AllIssues()
            {
                InitializeComponent();
                if (!ViewModelBase.IsInDesignModeStatic)
                {
                    // Use MEF To load the View Model
                    CompositionInitializer.SatisfyImports(this);
                }
            }
     
            [Import(ViewModelTypes.AllIssuesViewModel)]
            public object ViewModel
            {
                set
                {
                    DataContext = value;
                }
            }
       
    public void Cleanup()
            {
                // call Cleanup on its ViewModel
                ((ICleanup)this.DataContext).Cleanup();
                // call Cleanup on IssueEditor
                this.issueEditor.Cleanup();
                // Cleanup itself
                Messenger.Default.Unregister(this);
            }
     

    DispatcherHelper

    源代码中GalaSoft.MvvmLight.Test (SL).csproj 
    TestDispatcherHelper.cs有详细内容

    ViewModel

    从ViewModelBase继承实现视图对应的类
    
  • 相关阅读:
    转换进制,十六进制数相加
    一个人的旅行(Dijkstra算法)
    畅通工程续(Dijkstra算法)
    免费书下载
    http://d3js.org/
    React.js model
    jtable更新数据
    java knowledge record
    ActionListener三种实现
    java接口理解(转载)
  • 原文地址:https://www.cnblogs.com/2018/p/2045888.html
Copyright © 2011-2022 走看看