Background
For the MVVM architecture of the WP program, a very important problem is the navigation, as well as navigation parameters. Experienced people know that WP navigation can only be done in view, and navigation parameters can only be obtained in navigatedto and other view events, so we have to add processing in the Xaml.cs file to get the navigation parameters and then construct the ViewModel by navigation parameters. The process was painful because we had to switch back and forth in two files to see our logical code.
So next we'll look at the great thing about the CM writer (admittedly, really bad),
Let's look at what navigation looks like in the CM frame:
With no parameters:
Define the following functions in ViewModel:
The two lines cover a lot of information, and we first look at the questions that will exist when we first see these rows:
1, what is Navigatoinservice?
This member is defined in this way:
It is easy to see through the code that, by relying on injection, Mainpageviewmodel will get the object case of the Inavigationservice interface type (cm takes the container for object acquisition, As mentioned above, we define a container in the bootstrapper, where we do not unfold, and the reader clearly obtains the example.
2, what is Urifor?
CM's page navigation is based on viewmodel navigation, since we call Urifor<actionpractisepageviewmodel>, then after invoking Urifor, CM internal is automatically positioned within the Actionpractisepage.xaml defined in the project (note: This locates based on the naming convention, the naming rule needs to match the Apage.xaml->apageviewmodel) and automatically constructs the URI of the navigation.
3, Navigate?
This function is the action that the navigation performs, Once you find Actionpractisepage.xaml, you can navigate to this page, and CM will get Actionpractisepageviewmodel again through container, and ViewModel automatically binding to view.
4. What about the others?
In fact, the amazing thing is that we can even delete ActionPractisePage.xaml.cs, because the Actionpractisepage InitializeComponent () is automatically invoked after the navigation within CM. Complete the construction of the page. So our engineering catalog looks like this:-)
5. Do you need this line of code?
The previous reference to navigation will result in a case of viewmodel via container, so we need to configure Actionpractisepageviewmodel within bootstrapper:
Looking good, we can navigate and navigate based on ViewModel in ViewModel via the CM-provided navigation. The call is very concise, fluent interface makes the call is also very easy, error-prone.