使用 NavigationPage 时获取“iOS 上不支持全球范围内的 PushAsync ...”

Posted

技术标签:

【中文标题】使用 NavigationPage 时获取“iOS 上不支持全球范围内的 PushAsync ...”【英文标题】:Getting "PushAsync is not supported globally on iOS..." while USING NavigationPage 【发布时间】:2018-01-13 10:17:14 【问题描述】:

大家好,我不断收到以下信息:

System.InvalidOperationException with “iOS 全球不支持 PushAsync,请使用 NavigationPage。”

即使我使用的是NavigationPage 并且据我所知正在正确使用它。我的 F# 技能并不出色,所以我肯定认为我在这里做错了什么,但是,令人惊讶的是,在 F# 中很难找到执行 Navigation.PushAsync 的示例。我确实找到了这段代码,并一直在寻找正确的示例。

https://github.com/fsprojects/Tamarin/blob/master/samples/Todo/TodoListMvc.fs

在示例中,我看到此代码包含在 async

 do! this.Navigation.PushAsync(page = downcast view.Root) |> Async.AwaitIAsyncResult |> Async.Ignore

所以我不认为我离这里太远了。话虽如此,我做错了什么?

AssemblyVersions

Xamarin.Forms 版本 2.3.5.256-pre6 FSharp.Core 版本 4.0.0.1

代码

namespace Eclipsefest

open System
open Xamarin.Forms
open Xamarin.Forms.Xaml

type MenuItem = 
 Title : string
  TargetType : System.Type 

type MenuPage() = 
inherit ContentPage()
let _ = base.LoadFromXaml(typeof<MenuPage>)
member this.Handle_Tapped(sender : Object, arg : EventArgs) = 
    let navItem = sender :?> TextCell
    async  
        match navItem.Text with
        | "Information" -> 
            do! this.Navigation.PushAsync(page = new NavigationPage(new InfoPage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
        | "Events" -> 
            do! this.Navigation.PushAsync(new NavigationPage(new EventsPage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
        | "BusRoute" -> 
            do! this.Navigation.PushAsync(new NavigationPage(new BusRoutePage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
        | "Glasses" -> 
            do! this.Navigation.PushAsync(new NavigationPage(new GlassesPage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
        | "Merch" -> 
            do! this.Navigation.PushAsync(new NavigationPage(new MerchPage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
        | "Day" -> 
            do! this.Navigation.PushAsync(new NavigationPage(new DayPage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
        | "About" -> 
            do! this.Navigation.PushAsync(new NavigationPage(new AboutPage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
        | _ -> 
            do! this.Navigation.PushAsync(new NavigationPage(new EclipsefestPage()))
                |> Async.AwaitIAsyncResult
                |> Async.Ignore
    
    |> Async.RunSynchronously

代码更新

open System
open Xamarin.Forms
open Xamarin.Forms.Xaml

type MenuItem = 
     Title : string
      TargetType : System.Type 

type MenuPage() = 
    inherit ContentPage()
    let _ = base.LoadFromXaml(typeof<MenuPage>)
    member this.Handle_Tapped(sender : Object, arg : EventArgs) = 
        this.Navigation.PushAsync(new NavigationPage(new EclipsefestPage())) |> ignore

我也尝试了这一点并收到了同样的错误。我怀疑我的问题出在异步部分,但它似乎在没有异步的情况下也抛出了相同的异常。

代码更新 2

open System
open Xamarin.Forms
open Xamarin.Forms.Xaml

type MenuItem = 
     Title : string
      TargetType : System.Type 

type MenuPage() = 
    inherit ContentPage()
    let _ = base.LoadFromXaml(typeof<MenuPage>)
    member this.Handle_Tapped(sender : Object, arg : EventArgs) = 
        let page = new EclipsefestPage()
        let navpage = new NavigationPage(page)
        let result = this.Navigation.PushAsync(navpage)
        result.RunSynchronously()

我开始觉得这是一个错误。我已验证页面和导航页面在调用Navigation.PushAsync 之前已在各个步骤中实例化,并且仍然遇到相同的异常。

错误

"ios 全球不支持 PushAsync,请使用 NavigationPage。""

堆栈跟踪

System.InvalidOperationException: PushAsync is not supported globally on iOS, please use a NavigationPage.
  at Xamarin.Forms.Platform.iOS.Platform.Xamarin.Forms.INavigation.PushAsync (Xamarin.Forms.Page root, System.Boolean animated) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Platform.cs:154
  at Xamarin.Forms.NavigationProxy.OnPushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x00022] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationProxy.cs:190
  at Xamarin.Forms.NavigationProxy.PushAsync (Xamarin.Forms.Page root, System.Boolean animated) [0x00013] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationProxy.cs:109
  at Xamarin.Forms.NavigationProxy.OnPushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x00022] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationProxy.cs:190
  at Xamarin.Forms.NavigationProxy.PushAsync (Xamarin.Forms.Page root, System.Boolean animated) [0x00013] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationProxy.cs:109
  at Xamarin.Forms.NavigationProxy.OnPushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x00022] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationProxy.cs:190
  at Xamarin.Forms.NavigationProxy.PushAsync (Xamarin.Forms.Page root, System.Boolean animated) [0x00013] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationProxy.cs:109
  at Xamarin.Forms.NavigationProxy.PushAsync (Xamarin.Forms.Page root) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationProxy.cs:102
  at <StartupCode$Eclipsefest>.$MenuPage.xaml+Handle_Tapped@17.Invoke (Microsoft.FSharp.Core.Unit unitVar) [0x00090] in /Users/terrancesmith/Projects/Eclipsefest/Eclipsefest/MenuPage.xaml.fs:19
  at Microsoft.FSharp.Control.AsyncBuilderImpl+callA@851[b,a].Invoke (Microsoft.FSharp.Control.AsyncParams`1[T] args) [0x00047] in <5584acdb904cf4daa7450383dbac8455>:0
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151
  at Microsoft.FSharp.Control.AsyncBuilderImpl.commit[a] (Microsoft.FSharp.Control.AsyncBuilderImpl+Result`1[T] res) [0x00030] in <5584acdb904cf4daa7450383dbac8455>:0
  at Microsoft.FSharp.Control.CancellationTokenOps.RunSynchronously[a] (System.Threading.CancellationToken token, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x000a3] in <5584acdb904cf4daa7450383dbac8455>:0
  at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T] (Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout, Microsoft.FSharp.Core.FSharpOption`1[T] cancellationToken) [0x00071] in <5584acdb904cf4daa7450383dbac8455>:0
  at Eclipsefest.MenuPage.Handle_Tapped (System.Object sender, System.EventArgs arg) [0x00008] in /Users/terrancesmith/Projects/Eclipsefest/Eclipsefest/MenuPage.xaml.fs:16
  at Xamarin.Forms.Cell.OnTapped () [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Cells\Cell.cs:106
  at Xamarin.Forms.TextCell.OnTapped () [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Cells\TextCell.cs:82
  at Xamarin.Forms.TableView+TableSectionModel.OnRowSelected (System.Object item) [0x00007] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\TableView.cs:214
  at Xamarin.Forms.TableModel.RowSelected (System.Object item) [0x0001a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\TableModel.cs:65
  at Xamarin.Forms.TableModel.RowSelected (System.Int32 section, System.Int32 row) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\TableModel.cs:57
  at Xamarin.Forms.Platform.iOS.TableViewModelRenderer.RowSelected (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Renderers\TableViewModelRenderer.cs:85
  at 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/4691/d2270eec/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/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:63
  at Eclipsefest.iOS.Main.main (System.String[] args) [0x00001] in <59853c0a6da8a60ca74503830a3c8559>:0

【问题讨论】:

这是***.com/questions/45109867/… 的副本吗? (显然不同的语言) 请注意我已经在使用“new NavigationPage()”作为现有问题的推荐解决方案,但这并不能解决问题。 你找到的例子有用吗?如果是这样,我认为您最简单的做法是将工作示例慢慢转换为上面的代码,看看它何时会中断。 不,代码来自 F# MVC 框架 (Tamarin) 示例应用程序。这不是 1:1 的事情,但这是我发现的唯一在 F# 中使用 PushAsync 的事情。 这里有示例 xamarin 表单 F# 示例:developer.xamarin.com/guides/cross-platform/fsharp/samples 以及许多 PushAsync 示例,例如:github.com/xamarin/xamarin-forms-book-samples/blob/…。我认为这是你最好的选择。 【参考方案1】:

Xamarin 员工的回答。

在您显示的代码中,您正在包装要推送的新页面 在导航页中。但是,您要从 推送的页面是 需要在导航页面中。

最简单的方法是将初始页面包装在 NavigationPage 然后将其设置为 MainPage。然后你只需推新 直接页面而不将它们包装在另一个 NavigationPage 中,因为 这样做会导致其他问题。看看吧:

https://gist.github.com/jimmgarrido/d0b702c1282faa5bb91d0ed14e2d7e1b

namespace FForms

open System
open Xamarin.Forms

type TestPage() as this = 
    inherit ContentPage()

    let nextBtn = Button(Text = "Next Page")
    let layout = StackLayout()

    let handler = EventHandler(fun sender args ->
        let newPage = ContentPage(BackgroundColor = Color.Red)
        do this.Navigation.PushAsync(newPage))

    do nextBtn.Clicked.AddHandler handler
    do layout.Children.Add nextBtn
    do base.Content <- layout

type App() = 
    inherit Application(MainPage = NavigationPage(TestPage()))

希望这会有所帮助!

成功了~

【讨论】:

以上是关于使用 NavigationPage 时获取“iOS 上不支持全球范围内的 PushAsync ...”的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin QLPreviewController + NavigationPage 在 iOS 10 上损坏

为啥在使用 Prism 在 Xamarin.Forms 中导航到 root 时,我的 NavigationPage 顶部会出现默认标题?

Android 全球不支持 PushAsync,请使用 NavigationPage

Android 全球不支持推送异步,请使用 NavigationPage

将页面推送到最顶部的 NavigationPage

Xamarin - NavigationPage.TitleView 内的 StackLayout 不会使用所有空间?