简体   繁体   English

如何从ContentPage导航到ContentPage?

[英]How to navigate from ContentPage to ContentPage?

I am trying to navigate from one ContentPage to another. 我正在尝试从一个ContentPage导航到另一个。 In my WelcomePage , I have a button which should direct me to the other page with following content 在我的WelcomePage ,我有一个按钮,该按钮应将我定向到具有以下内容的另一页

        _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 现在,在我尝试管理所有页面的PageManager类中,我有类似的内容

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: 最后,在MainActivity.cs (Android项目)中,我尝试使用以下方法来管理所有这些:

//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. 我想要的是一种从一个ContentPage导航到另一个ContentPage的方法。

I share your frustration - however, I understand Xamarin are working on improving the page navigation. 我也很沮丧-但是,我了解Xamarin正在努力改善页面导航。

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. 如果您很高兴有流程,那么无需替换整个页面但可以返回,请使用提到的NavigationPage或PageManager echyzero。

If you are going to have an options page, use a MasterDetailPage and replace the detail. 如果要有一个选项页,请使用MasterDetailPage并替换详细信息。

Alternatively, create an interface which has a method called SetRootPage and implement it for both Android and iOS. 或者,创建一个接口,该接口具有一个名为SetRootPage的方法,并为Android和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. 在启动时将接口的实例传递到App.Run(IPageLoader)中,然后可以从App类中调用SetRootPage来替换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. 同时,我的解决方法是使用CarouselPage,在Carousel上只有一个页面,需要时可以替换该页面-实际上,如果有点黑,它的效果很好。

Answer taken from Xamarin.Forms documation here 答案来自Xamarin。 在此处形成文档

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) 对于iOS,您需要通过如下所示的NavigationPage进行此操作(示例位于AppDelegate.cs文件中)

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. 之后,您可以在应用程序的任何ContentPage中调用await Navigation.PushAsync('所需的任何页面')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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