从Xamarin Forms中的iOS中的App.OnSleep弹出导航堆栈

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Xamarin Forms中的iOS中的App.OnSleep弹出导航堆栈相关的知识,希望对你有一定的参考价值。

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

以下在我的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的Stacktrace:

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\_work1sXamarin.Forms.Platform.iOSPlatform.cs:113 
  at Xamarin.Forms.Internals.NavigationProxy.OnPopAsync (System.Boolean animated) [0x00007] in D:agent\_work1sXamarin.Forms.CoreNavigationProxy.cs:162 
  at Xamarin.Forms.Internals.NavigationProxy.PopAsync () [0x00000] in D:agent\_work1sXamarin.Forms.CoreNavigationProxy.cs:74 
  at InstMobile.App.ClearView () [0x00057] in C:UsersjmclachlanSourceReposInstGInstInstMobileApp.xaml.cs:66 
  at InstMobile.App.OnSleep () [0x0000c] in C:UsersjmclachlanSourceReposInstGInstInstMobileApp.xaml.cs:39 
  at Xamarin.Forms.Application.SendSleepAsync () [0x00000] in D:agent\_work1sXamarin.Forms.CoreApplication.cs:247 
  at Xamarin.Forms.Platform.iOS.FormsApplicationDelegate+OnResignActivation>d__8.MoveNext () [0x00023] in D:agent\_work1sXamarin.Forms.Platform.iOSFormsApplicationDelegate.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:UsersjmclachlanSourceReposInstGInstInstMobile.iOSMain.cs:17 

所以它在OnResignActivation中解雇。对于某些UI更改,这为时已晚吗?

答案

我做了两个似乎使这项工作变化的变化。我不知道哪个,但没关系。我使用了页面的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();
}

以上是关于从Xamarin Forms中的iOS中的App.OnSleep弹出导航堆栈的主要内容,如果未能解决你的问题,请参考以下文章

访问Xamarin Forms iOS应用程序中的共享存储位置

如何从 Xamarin.forms pcl 中的 Url 检索 json 字符串

如何从 Xamarin.forms pcl 中的 Url 检索 json 字符串

如何在 Xamarin Forms iOS 中的特定页面上导航?

Xamarin Forms - 状态栏与 IOS 中的内容重叠(safeareainsets 问题)

如何使Xamarin Forms App自动更新?