【问题标题】:Switching between pages causes crashes on Windows 10 Mobile在页面之间切换会导致 Windows 10 移动版崩溃
【发布时间】:2017-12-05 12:51:55
【问题描述】:

我正在使用 Xamarin Forms (2.3.4.247),而我的应用程序正在使用“HamburgerMenu”。要在页面之间切换,我正在使用它的代码:

private FirstPage firstPage; //it's i get from the constructor
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;
}

在 Android 和 Windows 10 桌面上一切正常,在 Windows 10 Mobile 模拟器上,当我在 thirdPage 和 firstPage 之间切换时,我的应用程序崩溃了。 FirstPage 是根目录:

FirstPage firstPage = new FirstPage();
NavigationPage = new NavigationPage(firstPage);

我不知道为什么...模拟器不允许调试...

第二件事: 当我将 Xamarin Forms 更新到版本 2.3.5.256-pre6 时,我的应用程序抛出异常“System.ArgumentException:'无法插入已经在导航堆栈中的页面'”......但是当我将代码更改为:

App.NavigationPage.Navigation.InsertPageBefore(new ThirdPage(), root);
App.NavigationPage.Navigation.InsertPageBefore(new SecondPage(), root);
//etc

一切正常... 有谁知道为什么会这样?我不想在页面切换时创建新对象...

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.uwp


    【解决方案1】:

    你已经回答了这个问题,所以我只能确认这一点:

    System.ArgumentException: 'Cannot insert page which is already in the navigation stack'
    

    如您所见,您已经在App.NavigationPage.Navigation 中拥有页面 firstPage,因此插入另一个页面会使应用程序崩溃。您只是不能按照您的解释执行此操作 - 要么您必须创建一个新实例,要么您必须从堆栈中删除以前的实例。

    【讨论】:

    • 是的,但为什么它没有在版本 2.3.4.247 上抛出?为什么应用只在 Win10Mobile 上崩溃?
    • Xamarin 正在将您的调用转换为原生类。每个操作系统都有其支持导航的本机类,它们的行为可能不同。它显然不适用于 Windows 10 移动版。为什么在以前版本的 Xamarin 中没有抛出?可能是因为某些东西没有按应有的方式工作,这也是 Xamarin 更新的原因之一。
    • 你可能是对的。那么... 应该使用什么代码才能在 Android 和 Windows Mobile 上工作? (现在我的代码只能在 Android 和 Windows 桌面上运行)。
    • 如前所述,如果您不想创建新实例,则可以检查当前实例是否存在于堆栈中并将其删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多