[MVVMLight Note] 2. Develop a simple chart Generation Program with source code

Source: Internet
Author: User

The previous article introduced how to quickly build a program named Ghost Based on MVVMLight.

The requirements are roughly as follows:

1. Users enter data of certain specifications

For example:

  • Buy car; 100000
  • Buy bike; 3000
  • Receive; 20000

2. You can customize the category and associate the index value (the keyword used to match the data) with the category.

For example:

  • Category name: buy a car, token value: car
  • Category name: buy a bicycle, token value: bike
  • Category name: Revenue, token value: receive

3. The program generates a pie chart based on the category and its associated indexes.

We will not analyze the specific logic. Here we will use the MVVM idea for development, and of course it should be based on MVVMLight.

First of all, we will design a home page, and then one page of the functions 1, 2, 3 will be embedded into the home page. Here we will use the tab to control the page switch, suppose our View has been designed.

Each View must have a ViewModel, and a ViewModel may contain other viewmodels. The ViewModel we want to develop will also be in this structure, as shown in figure

The main page is bound with a MainViewModel, while the MainViewModel also contains three viewmodels, which are used to bind the corresponding Tab page, such as category1 and category2, that is, Model.

App. xaml

              

Add ViewModelLocator resources to the App resource file as an example to implement the IOC function.

ViewModelLocator code

                    =><MainViewModel>                   ServiceLocator.Current.GetInstance<MainViewModel>

For more information about command binding, see how to bind a listbox command with a little complexity.

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

Corresponding

To delete an item, click X.

We can see that the Command is bound with RemoveCommand to see how it is implemented in the background.

                 RelayCommand RemoveCommand { ;  =              _parent.RemoveCategory(

Defines a RelayCommand, where is it holy?

   RelayCommand : ICommand

Do you know, it is a layer of ICommand packaging by MVVMLight

If you think about too much code and stick it out one by one, you can summarize it.

View is associated with ViewModel, and CRUD business logic is written in ViewModel. ViewModel Operates Model and Model carries data.

Previous

If you find it helpful to download the source code, let me enjoy it.

 

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.