我正在尝试在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\_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更改,这为时已晚吗?
答案 0 :(得分:0)
我进行了2次更改,似乎使这项工作成功。我不知道哪个,但没关系。我使用了页面的Navigation属性并删除了PopAsync()上的Wait()调用。这会导致在应用程序进入睡眠状态之前弹出窗口无法完成的时间问题吗?也许这是另一天的问题。
{{1}}