gpt4 book ai didi

android - PopAsync 导致 ArgumentOutOfRangeException

转载 作者:行者123 更新时间:2023-12-04 00:26:02 27 4
gpt4 key购买 nike

我对 Xamarin.Forms 还是很陌生。我在出现的主页上有一个非常简单的功能

async public Task BaseAppearing()
{
if (UserID == null)
{
var page = new LoginPage();
await App.Navigate(page);
}
else
{
stopWatch.Restart();
}
}

现在登录页面显示正常,问题是在登录页面中,当我点击登录并尝试弹出导航堆栈时,出现以下错误

Unhandled Exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

应用崩溃了。

异常仅在 Android(模拟器和真实)中发生。在 UWP 中它工作得很好

弹出代码也很简单

public static Task PopNavigation()
{
var nav = Current.MainPage.Navigation;
Task ret;
try
{
ret = nav.PopAsync();
}
catch (Exception ex)
{
ex.Log();
ret = Navigate(Current.MainPage);
}
return ret;
}

它不仅抛出错误,而且永远不会到达异常处理程序。

另外,我了解到有时导航堆栈会损坏/空。但是没有,NavigationStack 有 2 个项目。 MainPage 和 LoginPage,我认为是正确的

所有导航都发生在同一个堆栈上,因为我在应用程序中只有 1 个导航功能,而这就是我为导航所调用的全部

请告知可以做什么

更新我尝试更改我的代码以使用 NavigationPage 如下,但没有帮助(我也得到了顶部的 2 个标题栏)

public async static Task Navigate(Page Page)
{
//await Current.MainPage.Navigation.PushAsync( Page);
await Current.MainPage.Navigation.PushAsync( new NavigationPage( Page));
}

如果这可能对某人有所帮助,这是我的堆栈跟踪

at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:180 at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.GetFragment (Xamarin.Forms.Page page, System.Boolean removed, System.Boolean popToRoot) [0x00003] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:865 at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync (Xamarin.Forms.Page page, System.Boolean animated, System.Boolean removed, System.Boolean popToRoot) [0x0001a] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:768 at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPopViewAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x00017] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:592 at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.PopViewAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:154 at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPopped (System.Object sender, Xamarin.Forms.Internals.NavigationRequestedEventArgs e) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:573 at Xamarin.Forms.NavigationPage+d__69.MoveNext () [0x0004c] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:297 --- End of stack trace from previous location where exception was thrown --- at Xamarin.Forms.NavigationPage+d__68.MoveNext () [0x0004c] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:280 --- End of stack trace from previous location where exception was thrown --- at Xamarin.Forms.NavigationPage+d__45.MoveNext () [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:168 --- End of stack trace from previous location where exception was thrown --- at Qleeg.LoginPage+d__2.MoveNext () [0x003f8] in D:\Projects\Qleeg\Qleeg\Qleeg\Views\LoginPage.xaml.cs:70 --- End of stack trace from previous location where exception was thrown --- at (wrapper dynamic-method) System.Object.30(intptr,intptr) at (wrapper native-to-managed) System.Object.30(intptr,intptr)

我做错了什么?

更新 2

这是应用的构造函数

public App()
{
InitializeComponent();

AppDomain.CurrentDomain.UnhandledException += (sender, args) => OnError(sender, args.ExceptionObject as Exception);
TaskScheduler.UnobservedTaskException += (sender, args) => OnError(sender, args.Exception);
MainPage = new NavigationPage(new MainPage());
......
}

这是我的导航代码

从主页面到登录页面

async public static Task BaseAppearing(this ContentPage Input)
{
if (App.Current.UserID == null)
{
var page = new LoginPage();
await Input.Navigation.PushAsync(page);
}
else
{
App.Current.stopWatch.Restart();
}
}

回来

lblMessage.Text = "Welcome!";
lblMessage.TextColor = Color.Green;
Functions.WriteLog("Login", "pre nav pop");
//await App.PopNavigation();// App.Current.MainPage.Navigation.PopAsync();
await Navigation.PopAsync();//THIS IS LINE 70

更新 3

我将我的 MainPage 代码切换为:

    async private void MainPage_Appearing(object sender, EventArgs e)
{
if (App.Current.UserID == null) {await App.Current.MainPage.Navigation.PushAsync(new NavigationPage(new LoginPage ())); }
}

现在,错误消失了(尽管我对原因感到困惑),但 Navigaion.PopAsync() 根本什么也没做。没有错误,但也没有导航。

此外,LoginPage 现在显示顶部有 2 个标题行/标题。

最佳答案

添加

Task.Delay(300)

在导航页面之前。

我知道这是一个 hack但是如果您想在出现时导航到其他页面,那么我们必须这样做。导致整个页面尚未初始化。所以我们必须等待几秒钟才能在导航堆栈中正确初始化页面

关于android - PopAsync 导致 ArgumentOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56792068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com