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.