In fact, it is very easy to implement page jump in ViewModel. The following code:
| The code is as follows: |
Copy code |
Using Microsoft. Phone. Controls; Var root = App. Current. RootVisual as PhoneApplicationFrame; Root. Navigate (new Uri ("/NextPage. xaml", UriKind. Relative )); |
Under normal task circumstances, these lines of code can be completed, but there is a problem, let's further discuss: to create a user login function, according to the normal business process, after the user enters the user name and password on a page, the program connects to the server to verify the validity of the user (this process requires network connection, the process may take a long time, and it is an asynchronous operation ), when the server returns data, the program determines that if the user is a valid user, it will jump to the user's personal information interface. Otherwise, an error message is displayed.
The code for selecting behavior after VIEWMODEL processes the data returned by the server should be as follows:
| The code is as follows: |
Copy code |
If (result. IsSuccess = true) { Var root = App. Current. RootVisual as PhoneApplicationFrame; Root. Navigate (new Uri ("/UserInfo. xaml", UriKind. Relative )); } Else { // Display error information. }
|
However, to connect to the server, you need to connect to the network. If the network signal is poor, you may click "log on, after 10 seconds, you will not receive any success or failure prompt (a "logon in progress" may be displayed if you have a better experience "), the user is impatient and presses the back key or other operations to enter another page. Suddenly, after the server's feedback data is returned, the ViewModel code above forcibly brings the user to the personal information page, so, the user is very angry, and the consequences are very serious.
Just add a judgment:
| The code is as follows: |
Copy code |
If (result. IsSuccess = true) { Var root = App. Current. RootVisual as PhoneApplicationFrame; If (root. CurrentSource = new Uri ("Login. xaml", UriKind. Relative )) Root. Navigate (new Uri ("UserInfo. xaml", UriKind. Relative )); } Else { // Display error information } |