MVVM Light learning notes 2 for use

Source: Internet
Author: User
Tags blank page
After the installation is complete, we can directly create a project based on the MVVM Light template through VS2010 or blend4. The initial project created through the Template already has a simple example. Let's study it first. Compared with the general wp7 project, it creates two folders by default: Model and ViewModel. The ViewModel folder contains two simple code files. The specific content will be discussed later.

By default, the created MainPage. xaml page is a blank page without any content. However, there is an error message. (It is strange that there is an error prompt upon creation ). Compile at this time and find that something strange has happened. Some content is displayed on the MainPage. xaml page. (Haha, I am a layman ). After checking the relevant information, we found that the MVVM Light template actually hides many things and it helped us create them. However, we still need to know: 1. App. A reference for the ViewModel namespace is added to xaml, that is, the reference for the ViewModel namespace is added in the Application segment. Xmlns: vm = "clr-namespace: MvvmLight6.ViewModel"

In this example, xmlns indicates the xml namespace.

2. In addition. resources>, the following resource is registered: <vm: ViewModelLocator x: Key = "Locator" d: isDataSource = "True"/> the vm is the namespace defined previously. ViewModelLocator is a class in the namespace, marking its Key as Locator, and determining it as a data source. There is also a way to write resources directly here, instead of declaring the namespace in the first step: <vm: ViewModelLocator xmlns: vm = "clr-namespace: MvvmLight6.ViewModel" x: key = "Locator"/> You can also reference resources on a specific page. 3. The next step is to bind the resource to the page. <phone: PhoneApplicationPage
......
DataContext = "{Binding Main, Source = {StaticResource Locator }}"
......
> Here, Main is an attribute in the Locator (that is, the ViewModelLocator class). You can check the code to see that it is MainViewModel object. 4. All the controls on the page can be directly bound using the attributes in Main. The default code is as follows: <TextBlock x: Name = "ApplicationTitle"
Text = "{Binding ApplicationTitle }"
Style = "{StaticResource PhoneTextNormalStyle}"/> the Binding method is to directly write a Binding in brackets, followed by the corresponding attribute name ApplicationTitle. This is what I saw at the beginning of the article. These resources do not exist before compilation. After compilation, these resources can be properly bound. After learning about the above four aspects, I should have a clear impression on MVVM. However, this is just a fur, and the event binding has not yet appeared. Publish via Wiz

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.