【问题标题】:Popping the navigation stack from App.OnSleep in iOS in Xamarin Forms在 Xamarin Forms 中从 iOS 中的 App.OnSleep 弹出导航堆栈
【发布时间】:2018-03-03 14:38:53
【问题描述】:

我正在尝试在 App.OnSleep 方法中的 Xamarin Forms 应用中将当前视图页面从导航堆栈中弹出。当应用程序“暂停”时会触发此方法。 (暂停似乎映射到不同平台上的 1 个或多个事件。)

以下内容在我的 UWP 应用程序中运行良好(我只会在它是特定页面时弹出):

var currentPage = this.MainPage as NavigationPage; // MainPage = new NavigationPage(new MainPage())
if (currentPage != null && currentPage.CurrentPage is FooPage)
{
    // FooPage was shown from MainPage with Navigation.PushAsync(new FooPage())
    var fooPage = currentPage.CurrentPage as FooPage;
    currentPage.PopAsync().Wait();
}

当我暂停和恢复 UWP 应用时,之前的导航页面可见。在 iOS (11.2) 应用中,FooPage 仍然可见,但其上的控件没有响应。

我尝试使用 NavigationProxy.PopAsync.Wait(),但在 iOS 中出现异常。

这只是 Xamarin Forms 的 OnSleep 没有在 iOS 上提供足够支持的一个例子吗?还是 OnSleep 在我无法做到的 iOS 事件中触发?还是因为我是 Xamarin 的新手而错过了一些基本的东西?

更新:

来自 NavigationProxy.PopAsync 的堆栈跟踪:

System.InvalidOperationException: PopAsync is not supported globally on iOS, please use a NavigationPage.
  at Xamarin.Forms.Platform.iOS.Platform.Xamarin.Forms.INavigation.PopAsync (System.Boolean animated) [0x00000] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:113 
  at Xamarin.Forms.Internals.NavigationProxy.OnPopAsync (System.Boolean animated) [0x00007] in D:\agent\_work\1\s\Xamarin.Forms.Core\NavigationProxy.cs:162 
  at Xamarin.Forms.Internals.NavigationProxy.PopAsync () [0x00000] in D:\agent\_work\1\s\Xamarin.Forms.Core\NavigationProxy.cs:74 
  at InstMobile.App.ClearView () [0x00057] in C:\Users\jmclachlan\Source\Repos\InstG\Inst\InstMobile\App.xaml.cs:66 
  at InstMobile.App.OnSleep () [0x0000c] in C:\Users\jmclachlan\Source\Repos\InstG\Inst\InstMobile\App.xaml.cs:39 
  at Xamarin.Forms.Application.SendSleepAsync () [0x00000] in D:\agent\_work\1\s\Xamarin.Forms.Core\Application.cs:247 
  at Xamarin.Forms.Platform.iOS.FormsApplicationDelegate+OnResignActivation>d__8.MoveNext () [0x00023] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\FormsApplicationDelegate.cs:71 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
  at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24 
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Use
rs/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/Foundation/NSAction.cs:163 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at InstMobile.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\jmclachlan\Source\Repos\InstG\Inst\InstMobile.iOS\Main.cs:17 

所以它在 OnResignActivation 中触发。对于某些 UI 更改来说,这是否为时已晚?

【问题讨论】:

  • OnSleep 是从UIApplicationDelegateapplicationWillResignActive 触发的,虽然我已经“清理”了Xamarin.iOS(和ObjC/Swift)中的UI,但我没有在表格中完成。有一些 rdars 关于在此方法中执行 UIKit 工作,大多数是通过辞去第一响应者(Xamarin.Forms 不这样做)来解决的。那么你得到的Exception(和 StackTrace)究竟是什么?键盘是否显示?
  • 添加了 NavigationProxy.PopAsync 的堆栈跟踪。不,当我调用任一 PopAsync 方法时,UI 中都没有显示键盘。什么是 rdar?
  • 您的错误与它无关,开始在OnSleep 内使用,如错误所述,PopAsync/PushAsync/PopToRootAsync 在 iOS 上不受全球支持。您需要将上下文页面包装在 NavigationPage 中,即stackoverflow.com/a/45120136/4984832radr 是 Apple 错误报告)

标签: ios xamarin xamarin.forms xamarin.ios


【解决方案1】:

我进行了 2 项更改,似乎使这项工作有效。我不知道是哪个,但没关系。我使用了页面的 Navigation 属性并删除了 PopAsync() 上的 Wait() 调用。这会导致在应用程序进入睡眠之前弹出没有完成的时间问题吗?也许这是另一天的问题。

var currentPage = this.MainPage as NavigationPage; // MainPage = new NavigationPage(new MainPage())
if (currentPage != null && currentPage.CurrentPage is FooPage)
{
    // FooPage was shown from MainPage with Navigation.PushAsync(new FooPage())
    currentPage.Navigation.PopAsync();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多