C# 函数返回值中的F#类型错误

C# 函数返回值中的F#类型错误,c#,ios,.net,f#,xamarin.ios,C#,Ios,.net,F#,Xamarin.ios,我正在尝试用F#实现以下代码片段: 我已经做了以下工作: let toggleKeyboard(notification : NSNotification) = Console.WriteLine ("Received a notification UIKeyboard", notification) NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, toggleKeyboa

我正在尝试用F#实现以下代码片段:

我已经做了以下工作:

let toggleKeyboard(notification : NSNotification) = 
    Console.WriteLine ("Received a notification UIKeyboard", notification)

NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, toggleKeyboard) |> ignore
这似乎是一个简单的实现,但是我得到一个类型错误:

此表达式应具有类型“Action”,但此处具有类型“a->unit”(FS0001)(论文)

我真的不知道如何使我的方法返回
Action
类型

有时F#会自动将函数强制转换为
Action
Func
类型,但您也可以显式地将函数包装在
Action
中,如下所示:

let foo x = printfn "%s" x
let action = System.Action<string>(foo)
action.Invoke("hey") // prints hey
让foo x=printfn“%s”x
让动作=系统动作(foo)
action.Invoke(“hey”)//打印hey
或者在您的情况下:

...AddObserver(UIKeyboard.WillShowNotification, System.Action<NSNotification>(toggleKeyboard)) |> ignore
…AddObserver(UIKeyboard.WillShowNotification,System.Action(toggleKeyboard))|>忽略
...AddObserver(UIKeyboard.WillShowNotification, System.Action<NSNotification>(toggleKeyboard)) |> ignore