Xamarin. Forms + Prism (2) -- basically use NavigationService, xamarin. forms

Source: Internet
Author: User

Xamarin. Forms + Prism (2) -- basically use NavigationService, xamarin. forms

This article describes and uses the NavigationService In the Prism framework.

1. Open VS and you can see the installed templates on the left:

2. After creation, you can see the code in App. xaml. cs in the pcl project. The current logic of the program is to open APP-> NavigationPage-> MainPage (pass the title parameter );

3. Add a Prism ContentPage in the Views folder. You can see that LoginPageViewModel is automatically added to ViewModels.

 

4. If we want to modify the logic to open the APP now, we need to determine whether the last logon status is required to determine whether to jump to LoginPage:

Now we need to modify the logic and add code in OnInitialized () of App. xaml;

Protected override void OnInitialized () {InitializeComponent (); bool isLogin = false; // This is just a hypothetical value. In a real project, you should determine whether you have logged on. If (isLogin) {// you have logged on to NavigationService. NavigateAsync ("NavigationPage/MainPage? Title = Hello % 20 from % 20Xamarin. Forms ");} else {NavigationService. NavigateAsync (" LoginPage ");}}

Add a logon button in the Xaml of LoginPage. It can be seen that the MVVM design mode is used:

<Button Text = "Logon" Command = "{Binding LoginCommand}"> </Button>

 

 

Modify the code of LoginPageViewModel and create a new constructor. The parameter is INavigationService, which is the built-in navigation service of Prism and will be automatically transmitted;

Public class LoginPageViewModel: BindableBase {public LoginPageViewModel () {} private INavigationService _ navigationService; /// <summary> /// command bound to the logon operation /// </summary> private DelegateCommand <EventArgs> _ loginCommand; public DelegateCommand <EventArgs> LoginCommand {get {if (_ loginCommand = null) {_ loginCommand = new DelegateCommand <EventArgs> (async r => {// log on here, for example, access your WebApi await Task. delay (2000); // if you have completed the logon operation, save the user information and jump to MainPage; await _ navigationService. navigateAsync ("NavigationPage/MainPage");} return _ loginCommand;} public LoginPageViewModel (INavigationService navigationService) {_ navigationService = navigationService ;}}

If we do this now, after successfully logging on to the user and clicking logon, the user will jump to NavigationPage-> MainPage. After clicking back, the user will jump back to the logon page. In this case, we need to manually set the stack in NavigationService.

Modify the jump code to so that if the user clicks back, it will not return to the MainPage.

    await _navigationService.NavigateAsync("app:///NavigationPage/MainPage");    //await _navigationService.NavigateAsync("NavigationPage/MainPage");

 

In addition, if the ViewModel class implements the INavigationAware interface, three methods will be implemented: navigation preparation, navigation entry, and export jump out of three basic class methods.

        public void OnNavigatedFrom(NavigationParameters parameters)        {        }        public void OnNavigatingTo(NavigationParameters parameters)        {        }        public void OnNavigatedTo(NavigationParameters parameters)        {            if (parameters.ContainsKey("title"))                Title = (string)parameters["title"] + " and Prism";        }

  

Finally:

1. Note that all pages to be used by the program must be registered in the App. xaml code. Because the Prism service is Page-based, the registered object base class must be Page

  

2. When NavigationService is used for navigation, the string Page name must be case-insensitive. If it is written as mainpage, an error is returned.

 

3. Run the program: The program runs and jumps directly to LoginPage. Click the logon button. The program will jump to MainPage after 2 seconds. Now we can test and click back to exit the program. The logic is modified successfully.

 

 

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.