【问题标题】:How to switch between pages with Xamarin.Forms.INavigation.InsertPageBefore如何使用 Xamarin.Forms.INavigation.InsertPageBefore 在页面之间切换
【发布时间】:2017-12-08 00:35:21
【问题描述】:

我正在使用 Xamarin Forms,但在将 InsertPageBefore() 方法与 Pages 的现有对象结合使用时遇到问题。

这是我的查看代码:

private FirstPage firstPage; 
private SecondPage secondPage = new SecondPage();
private ThirdPage thirdPage = new ThirdPage(); 
private async void ItemSelectedMethod()
{
        var root = App.NavigationPage.Navigation.NavigationStack[0];
        if (SelectedItem == Items[0])
        {
            if (!IsFirstChoose)
            {
                App.NavigationPage.Navigation.InsertPageBefore(firstPage, root);
                await App.NavigationPage.PopToRootAsync(false);
            }
        }
        if (SelectedItem == Items[1])
        {
            App.NavigationPage.Navigation.InsertPageBefore(secondPage, root);
            await App.NavigationPage.PopToRootAsync(false);
        }
        if (SelectedItem == Items[2])
        {
            App.NavigationPage.Navigation.InsertPageBefore(thirdPage, root);
            await App.NavigationPage.PopToRootAsync(false);
        }

        IsFirstChoose = false;
        rootPageViewModel.IsPresented = false;
}

它抛出异常“System.ArgumentException:'无法插入已经在导航堆栈中的页面'”。如何在现有的页面对象之间切换?我不想在 InsertPageBefore() 中创建新对象。在调用 InsertPageBefore() 之前,我尝试使用它的代码:

foreach (var item in App.NavigationPage.Navigation.NavigationStack.ToList())
                App.NavigationPage.Navigation.RemovePage(item);

但它不起作用......谁能帮助我?

【问题讨论】:

  • stackoverflow.com/questions/44865472/… 的可能重复项您要达到什么目的?为什么你使用 Insert 和 Pop 而不是 Push?
  • 我想要它的菜单:csharp-dev.pl/wp-content/uploads/2017/04/5.png 当我点击项目时,他的页面显示为“根页面”。
  • 看起来您正在使用主/详细页面。你的 NavigationPage 详细吗?
  • 是的。 rootPage.Detail = 导航页;它的代码适用于 Android 的旧版 Xamarin,但不适用于新版 Xamain 和 Windows 10 Mobile。
  • 当我使用这一行时:App.NavigationPage.Navigation.InsertPageBefore(new ThirdPage(), root);一切正常,但我不想在 args 中为此方法(InsertPageBefore)创建新对象。

标签: c# xamarin xamarin.forms


【解决方案1】:

它不适用于 UWP。这是适合您的灵活解决方法,但您确实需要阅读如何使用主-详细信息页面。

public partial class App : Application
    {
        public static RootPage RootPage { get; private set; } //DON'T DO THIS, 
                                                              //FIND A BETTER WAY 
        public App()
        {
            InitializeComponent();

            RootPage = new RootPage();
            MenuPage menuPage = new MenuPage(RootPage.vm);

            RootPage.Master = menuPage;
            RootPage.Detail = new NavigationPage(new MainPage());// NavigationPage;
            MainPage = RootPage;
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

然后

private async void ItemSelectedMethod()
        {

            if (SelectedItem == Items[0])
            {
                App.RootPage.Detail = new NavigationPage(mainPage);
            }
            if (SelectedItem == Items[1])
            {
                App.RootPage.Detail = new NavigationPage(secondPage);
            }
            if (SelectedItem == Items[2])
            {
                App.RootPage.Detail = new NavigationPage(thirdPage);
            }
            rootPageViewModel.IsPresented = false;
        }

【讨论】:

  • 它正在工作!非常感谢您的帮助! (考虑到我的英语):)
  • 欢迎您,但不要照原样使用它 :-) 请阅读如何做得更好
  • PS:为什么我不应该使用静态 RootPage?
  • 一般来说使用静态变量不是一个好主意。你可以找到很多帖子为什么它不好。
  • 有时它们很有用,但我会寻找另一种方式:) 再次感谢你:)
猜你喜欢
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 2023-03-14
  • 2018-09-13
  • 2016-08-04
  • 1970-01-01
  • 2020-11-23
  • 2020-06-30
相关资源
最近更新 更多