MVVM Light framework (MVVM Mode)

Source: Internet
Author: User
The MVVM Light framework is an MVVM mode implementation for WPF and silverlight development. The following briefly summarizes the framework.

[The following is based on Silverlights4]

MVVM Light Composition

The current framework has two library files.

GalaSoft. MvvmLight Library

ViewModelBase: basic class of View Model, VM implementation in MVVM

Messenger: messages transmitted between ViewModel and View. Note that some frequently used Message Processing classes have been predefined in the GalaSoft. MvvmLight. Messaging namespace of the system,
Such as DialogMessage, icationicationmessageaction, notifmessmessagewithcallback, etc.

Command: Command, RelayCommand, language and Interface Element response binding

GalaSoft. MvvmLight. Extras Library

EventToCommand: bind Command with XAML, used in Expression4

DispatcherHelper: multi-thread processing

Install and use MVVM Light

GalaSoft.MvvmLight.Binaries.V3.zip

Decompress the package to the system's Program files directory.

GalaSoft.MvvmLight.Templates.V3.VS10.zip

Copy the Silverlight content under ItemTemplates ProjectTemplates to the corresponding

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

In this case, the MVVM light Template under Silverlight can be selected for the New Project. However, this template is relatively simple.

General Project usage:

1. Add GalaSoft. MvvmLight. SL4.dll GalaSoft. MvvmLight. Extras. SL4.dll reference

2. Add ViewModelLocator to App. xaml. Of course, you do not need to use the code or MEF to bind the View and ViewModel.

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

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

3. Add ViewModel and View

At this point:

ViewModel: Add Command implementation, send messages, and View transmission information

View: Data Binding, Command binding, message registration, and ICleanup are enabled for ViewModel clearing and registering message cleaning, interface and interaction design.
 
For the initialization association between View and ViewModel, you can use ViewModelLocator. cs to establish and bind [this form is still somewhat troublesome]. Of course, you can also use IOC such as MEF for automatic injection.

You can use

DataContext = "{Binding Main, Source = {StaticResource Locator}", you can also use code to bind [the Locator here is declared in App. xaml]

MVVM Light instance

Message Registration

It is generally registered in View to respond to messages in ViewModel. Of course, it can also be registered elsewhere.

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

Message sending

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

Command Definition

        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 binding

   <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 in the source code
TestDispatcherHelper. cs details

ViewModel

Inherit the class corresponding to the view from ViewModelBase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.