Yis*_*ski 3 navigation android xamarin.forms
我对Xamarin.Forms. 我在主页上有一个非常简单的功能出现
async public Task BaseAppearing()
{
if (UserID == null)
{
var page = new LoginPage();
await App.Navigate(page);
}
else
{
stopWatch.Restart();
}
}
Run Code Online (Sandbox Code Playgroud)
现在登录页面显示正常,问题是在登录页面中,当我单击登录并尝试弹出导航堆栈时,出现以下错误
未处理的异常:
System.ArgumentOutOfRangeException:索引超出范围。必须是非负的并且小于集合的大小。参数名称:索引
并且应用程序崩溃。
异常仅发生在 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;
}
Run Code Online (Sandbox Code Playgroud)
它不仅抛出错误,而且永远不会到达异常处理程序。
另外,我读到有时导航堆栈会损坏/为空。但是没有,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));
}
Run Code Online (Sandbox Code Playgroud)
如果这可能对某人有帮助,这是我的堆栈跟踪
在 System.Collections.Generic.List`1[T].get_Item(System.Int32 索引)[0x00009] 在 /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/ external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:180 在 Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.GetFragment(Xamarin.Forms.Page 页面,System.Boolean 已删除, System.Boolean popToRoot) [0x00003] 在 D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:865 在 Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync (Xamarin. Forms.Page 页面,System.Boolean 动画,System.Boolean 移除,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 页面,System.Boolean 动画)[0x00017] 在 D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:592 在 Xamarin.Forms.Platform .Android.AppCompat.NavigationPageRenderer.PopViewAsync(Xamarin.Forms.Page 页面,System.Boolean 动画)[0x00000] 在 D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:154 Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPopped(System.Object 发送者,Xamarin.Forms.Internals.NavigationRequestedEventArgs e)[0x00000] 在 D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat \NavigationPageRenderer.cs:573 在 Xamarin.Forms.NavigationPage+d__69.MoveNext () [0x0004c] 在 D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:297 --- 从上一个抛出异常的位置结束堆栈跟踪 --- 在 Xamarin.Forms.NavigationPage+d__68.MoveNext () [0x0004c] 在 D:\a\1\s\Xamarin.Forms.Core\NavigationPage .cs:280 --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 D:\a\1\s\Xamarin.Forms 中的 Xamarin.Forms.NavigationPage+d__45.MoveNext () [0x000cb]。 Core\NavigationPage.cs:168 --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 D:\Projects\Qleeg\Qleeg\Qleeg\Views 中的 Qleeg.LoginPage+d__2.MoveNext () [0x003f8] \LoginPage.xaml.cs:70 --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- at (wrapper dynamic-method) System.Object.30(intptr,intptr) at (wrapper native-to-managed ) System.Object.30(intptr,intptr)MoveNext () [0x0004c] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:280 --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 Xamarin.Forms.NavigationPage +d__45.MoveNext () [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:168 --- 从前一个抛出异常的位置开始的堆栈跟踪结束 --- 在 Qleeg。 LoginPage+d__2.MoveNext () [0x003f8] in D:\Projects\Qleeg\Qleeg\Qleeg\Views\LoginPage.xaml.cs:70 --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 (包装器动态方法) System.Object.30(intptr,intptr) at (wrapper native-to-managed) System.Object.30(intptr,intptr)MoveNext () [0x0004c] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:280 --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 Xamarin.Forms.NavigationPage +d__45.MoveNext () [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:168 --- 从前一个抛出异常的位置开始的堆栈跟踪结束 --- 在 Qleeg。 LoginPage+d__2.MoveNext () [0x003f8] in D:\Projects\Qleeg\Qleeg\Qleeg\Views\LoginPage.xaml.cs:70 --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 (包装器动态方法) System.Object.30(intptr,intptr) at (wrapper native-to-managed) System.Object.30(intptr,intptr)NavigationPage+d__45.MoveNext () [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:168 --- 从上一个抛出异常的位置开始的堆栈跟踪结束---在 Qleeg .LoginPage+d__2.MoveNext () [0x003f8] in D:\Projects\Qleeg\Qleeg\Qleeg\Views\LoginPage.xaml.cs:70 --- 从上一个抛出异常的位置开始的堆栈跟踪结束---在(包装器动态方法) System.Object.30(intptr,intptr) at (wrapper native-to-managed) System.Object.30(intptr,intptr)NavigationPage+d__45.MoveNext () [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:168 --- 从上一个抛出异常的位置开始的堆栈跟踪结束---在 Qleeg .LoginPage+d__2.MoveNext () [0x003f8] in D:\Projects\Qleeg\Qleeg\Qleeg\Views\LoginPage.xaml.cs:70 --- 从上一个抛出异常的位置开始的堆栈跟踪结束---在(包装器动态方法) System.Object.30(intptr,intptr) at (wrapper native-to-managed) System.Object.30(intptr,intptr)70 --- 从上一个抛出异常的位置结束堆栈跟踪 --- 在(包装器动态方法)System.Object.30(intptr,intptr)在(包装器本机到托管)System.Object.30( intptr,intptr)70 --- 从上一个抛出异常的位置结束堆栈跟踪 --- 在(包装器动态方法)System.Object.30(intptr,intptr)在(包装器本机到托管)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());
......
}
Run Code Online (Sandbox Code Playgroud)
这是我的导航代码
从 MainPage 到 LoginPage
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后回来
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
Run Code Online (Sandbox Code Playgroud)
更新 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 ())); }
}
Run Code Online (Sandbox Code Playgroud)
现在,错误消失了(尽管我对原因感到困惑),但Navigaion.PopAsync()什么也不做。没有错误,但也没有导航。
此外,LoginPage 现在出现在顶部的 2 个标题行/标题。
添加
Task.Delay(300)
在导航页面之前。
我知道这是一个黑客,但如果你想在出现时导航到其他页面,那么我们必须这样做。因为整个页面尚未初始化。所以我们必须等待几秒钟才能在导航堆栈中正确初始化页面
| 归档时间: |
|
| 查看次数: |
1694 次 |
| 最近记录: |