简体   繁体   中英

How to navigate from ContentPage to ContentPage?

I am trying to navigate from one ContentPage to another. In my WelcomePage , I have a button which should direct me to the other page with following content

        _loginButton.Clicked += (s, e) =>
            {
                if (OnLoginEnter != null) OnLoginEnter();
            };

Now, in my PageManager class, where I try to manage all pages, I have something like this

public class PageManager : Page
{
    #region Fields
    public static WelcomePage WelcomePage = new WelcomePage();
    public static LoginPage LoginPage = new LoginPage();
    #endregion

    public Page GetStarted()
    {
        return WelcomePage.Generate();
    }
}

And finally, in MainActivity.cs (Android project), I am trying to manage all this with the following:

//I have to use a method for that for some reason. Can't call .Generate() directly.
SetPage(PageManager.GetStarted());
PageManager.WelcomePage.OnLoginEnter += () =>
    {
        SetPage(PageManager.LoginPage.Generate());
    };

I find this to be very confusing and unproductive. All I want is a way to navigate from one ContentPage to another.

I share your frustration - however, I understand Xamarin are working on improving the page navigation.

For now, you have a few options.

If you are happy to have a flow, so no need to replace the whole page but be able to go back, use a NavigationPage or the PageManager echyzero mentioned.

If you are going to have an options page, use a MasterDetailPage and replace the detail.

Alternatively, create an interface which has a method called SetRootPage and implement it for both Android and iOS. Pass the instance of the interface in to your App.Run(IPageLoader) on startup and you can then call the SetRootPage from the App class to replace the Root. I did report a bug with this a while ago, which may have been fixed now. In the meantime, my workaround was to use the CarouselPage, with only a single page on the Carousel, which I replace when needed - it actually works quite well, if a bit hacky.

Answer taken from Xamarin.Forms documation here

listView.ItemSelected += async (sender, e) => {
    var todoItem = (TodoItem)e.SelectedItem;
    var todoPage = new TodoItemPage(todoItem); // so the new page shows correct data
    await Navigation.PushAsync(todoPage);
};

So for you, it would probably look something like this:

PageManager.WelcomePage.OnLoginEnter += () =>
    {
        await Navigation.PushAsync(PageManager.LoginPage.Generate());
    };

For iOS, you will need to do this through a NavigationPage as shown below (example is inside the AppDelegate.cs file)

window.RootViewController = new NavigationPage (new MyLoginForm ()).CreateViewController ();

After that you can call await Navigation.PushAsync('any page you want') inside any ContentPage in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM