无法在Xamarin.Forms中使用F#异步支持观察页面导航
我无法观察页面导航 命令:无法在Xamarin.Forms中使用F#异步支持观察页面导航,xamarin.forms,f#,Xamarin.forms,F#,我无法观察页面导航 命令: member x.AddBankcard = DelegateCommand( (fun _ -> async { do! navigate() } |> Async.RunSynchronously |>
member x.AddBankcard = DelegateCommand( (fun _ -> async { do! navigate()
} |> Async.RunSynchronously
|> ignore ) ,
fun _ -> true) :> ICommand
发布导航请求:
let navigate() =
account
|> PageRequest.AddBankcard
|> broadcastToAsync pageRequesthandlers
通知订户请求:
let broadcastToAsync<'a> (handlers:(list<'a -> Async<unit>>)) (msg:'a) : Async<unit> =
async { handlers |> List.map (fun handle -> async { do! handle msg }) |> ignore }
注意:
let navigationPage = (app:?>Application).MainPage:?>NavigationPage
let navigate () = async {
do! navigationPage.PushAsync(pageRequest) |> awaitTask
}
navigate() |> Async.RunSynchronously |> ignore
我可以告诉您,
broadcastToAsync
功能实际上不执行任何操作:
let broadcastToAsync<'a> (handlers:(list<'a -> Async<unit>>)) (msg:'a) : Async<unit> =
async { handlers |> List.map (fun handle -> async { do! handle msg }) |> ignore }
以下代码起作用:
let navigationPage = (app:?>Application).MainPage:?>NavigationPage
navigationPage.PushAsync(pageRequest) |> Async.AwaitTask |> ignore
以下是从堆栈中弹出页面的一些代码:
app |> function
| :? Application as xForms ->
let navigationPage = xForms.MainPage:?>NavigationPage
navigationPage.PopAsync() |> Async.AwaitTask |> ignore
只有在异步/任务操作完成后必须执行代码时,才需要
async{…}
块
对于导航,之后通常无需执行任何操作(一些MVVM框架除外)。ignore
是必需的,因为函数签名要求返回单元,导航方法返回任务。在C#中,调用时会启动任务
,而F#中的异步
块则不会启动
下面的代码是一个非常简单的导航示例,不需要async
关键字或async.waittask
等
open Xamarin.Forms
open Xamarin.Forms.Xaml
type NextPage(app:Application) =
inherit ContentPage()
let button = Button(Text = "Back")
do
button.Clicked.Add (fun _ -> app.MainPage.Navigation.PopAsync () |> ignore )
base.Content <- nextButton
type MainPage(app:Application) =
inherit ContentPage()
let nextButton = Button(Text = "Next")
do
nextButton.Clicked.Add (fun _ ->
let nextPage = NextPage app
app.MainPage.Navigation.PushAsync nextPage |> ignore )
base.Content <- nextButton
type App() as this =
inherit Application()
do this.MainPage <- NavigationPage(MainPage(this))
打开Xamarin.Forms
打开Xamarin.Forms.Xaml
键入下一页(应用程序:应用程序)=
继承ContentPage()
让按钮=按钮(Text=“后退”)
做
button.Clicked.Add(乐趣->app.MainPage.Navigation.PopAsync()|>忽略)
基本内容
让nextPage=nextPage应用程序
app.MainPage.Navigation.PushAsync下一页|>忽略)
基本内容,谢谢。但我仍然在以下方面遇到异常:async{navigationPage.PushAsync(pageRequest)|>async.AwaitTask |>async.RunSynchronously |>ignore}您收到了什么消息?我的应用程序现在挂起了:navigationPage.PushAsync(pageRequest)|>async.AwaitIAsyncResult |>async.ignore
app |> function
| :? Application as xForms ->
let navigationPage = xForms.MainPage:?>NavigationPage
navigationPage.PopAsync() |> Async.AwaitTask |> ignore
open Xamarin.Forms
open Xamarin.Forms.Xaml
type NextPage(app:Application) =
inherit ContentPage()
let button = Button(Text = "Back")
do
button.Clicked.Add (fun _ -> app.MainPage.Navigation.PopAsync () |> ignore )
base.Content <- nextButton
type MainPage(app:Application) =
inherit ContentPage()
let nextButton = Button(Text = "Next")
do
nextButton.Clicked.Add (fun _ ->
let nextPage = NextPage app
app.MainPage.Navigation.PushAsync nextPage |> ignore )
base.Content <- nextButton
type App() as this =
inherit Application()
do this.MainPage <- NavigationPage(MainPage(this))