[MVVMLight Note] 1. quickly build a silverlight Applet based on MVVMLight

Source: Internet
Author: User

I wrote an MVVM note example.

I suppose you already have some MVVM concepts, so we just went straight into it. How can we build projects based on MVVMLight? In fact, it is very simple. First we need to download MVVMLight and install it. After installation, you will see

Binaries has various versions of the Assembly

Here I used the assembly of silverlight4.

However, if you have installed the template and created a template project, you do not need to manually add the above Assembly. I use VS2012, so the following second template MvvmLight. VS2012.vsix is installed.

Now you can create a new project. In the new project silverlight option, you will see two more options.

I used SL4, so I chose the first new one.

We can see a general shelf to help you build it out. Press F5 and we will see

 

Congratulations! Your first MVVMLight application is complete !!! Let's see what we have written.

Find the App entry

                         

We can see that ViewModelLocator has been added to an application resource. What is the use of ViewModelLocator? Let's look at what is written in App. cs.

    Application_Startup(=   Application_Exit(

Compared with the previous code, we saw ViewModelLocator again in the exit of the program. It seems that this is always throughout our application and will be discussed later. We also found that DispatcherHelper. Initialize () was added during startup. What is initialization?

   (UIDispatcher == =  

Have you seen it? It will get the Dispatcher of the current application. What is the Dispatcher used? Cross-thread operations are required.

Okay, our program RootVisual has set MainPage. Let's see what MainPage has.

                                                                                  

Isn't it strange that the MainPage in the App has not assigned a value to DataContext?

DataContext="{Binding  Main, Source={StaticResource Locator}}"

Locator is it familiar ~ By the way, it is added to the resource file of the App. Now let's take a look at the sacred role of ViewModelLocator.

  =><IDataService, Design.DesignDataService><IDataService, DataService><MainViewModel>                   ServiceLocator.Current.GetInstance<MainViewModel>                    
ServiceLocatorAnd

This class provides the ambient container for this application. If your framework
Defines such an ambient container, use ServiceLocator. Current to get it.

Since it is a container, why? Continue to read the code, ServiceLocator. setLocatorProvider () => SimpleIoc. default); sets a delegate SimpleIoc for this container. default, may see IOC, immediately think of control inversion, dependency injection. Yes ~ SimpleIoc is an IOC container implemented by MVVMLight. Let's look at the SimpleIoc. Default. Register Method, and you will understand that it was originally to plug something into the container (interface implementation, class ).

If there is a put, there is a get

 ServiceLocator.Current.GetInstance<MainViewModel>

This is also the property bound to the page.

Now we only see the attributes of ViewModel. Well, let's see what ViewModel is like.

                         WelcomeTitlePropertyName =   _welcomeTitle =                     (_welcomeTitle ===                  ==> (error !=                         =    }

It inherits ViewModelBase. In fact, it implements INotifyPropertyChanged and System. ComponentModel. INotifyPropertyChanging.

Didn't you find the Model? oh, isn't WelcomeTitle a Model?

So far today, you can download the source code if it is helpful.

Related Article

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.