Use NavigationService in ViewModel in wp7

Source: Internet
Author: User

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
            }
 

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.