C# F中没有参数的Lambda表达式#
我正在尝试将以下C代码转换为F代码: 它调用C# F中没有参数的Lambda表达式#,c#,lambda,f#,C#,Lambda,F#,我正在尝试将以下C代码转换为F代码: 它调用UIView.Animate,并传递动画代码和动画完成代码的持续时间、延迟、动画选项和lambda表达式 我知道F#中的lambda表达式使用fun语法。这是我的尝试: UIView.Animate(0,UIViewAnimationOptions.CurveEaseOut, fun -> { //first code section }, fun ->
UIView.Animate
,并传递动画代码和动画完成代码的持续时间、延迟、动画选项和lambda表达式
我知道F#中的lambda表达式使用fun
语法。这是我的尝试:
UIView.Animate(0,UIViewAnimationOptions.CurveEaseOut, fun ->
{
//first code section
},
fun ->
{
//second code section
}
)
但我认为我做得不对,因为它在lambda表达式中抛出了语法错误意外符号->
编辑:
这是我的代码片段,它是嵌套在另一个动画中的动画。我得到的错误是该方法或对象动画不包含4个参数,发现一个方法或重载包含两个参数。我试图删除fun
块之间的逗号,但是它抱怨缩进
UIView.Animate(0,UIViewAnimationOptions.CurveEaseOut, (fun () ->
x.View.LayoutIfNeeded()),
fun () -> (
let height : nfloat = Conversions.nfloat(220)
let animationDuration = (userInfo.[UIKeyboard.AnimationDurationUserInfoKey] :?> NSNumber).FloatValue
let animationOptions = (userInfo.[UIKeyboard.AnimationCurveUserInfoKey] :?> NSNumber).UintValue
let window = UIApplication.SharedApplication.KeyWindow
if window <> null then
if isKeyboardShowing then
Console.WriteLine("Completed")
let y = window.Frame.Height
UIView.Animate(new TimeInterval(animationDuration),UIViewAnimationOptions(animationOptions), fun () ->
bottomView.Frame <- CGRect(0,y,bottomView.Frame.Width,bottomView.Frame.Height)
fun () ->
Console.WriteLine("Completed")
)
))
UIView.Animate(0,uiviewmanimationoptions.CurveEaseOut,(乐趣()->
x、 View.layoutifneed()),
乐趣()->(
let height:nfloat=Conversions.nfloat(220)
让animationDuration=(userInfo.[UIKeyboard.AnimationDurationUserInfoKey]:?>NSNumber).FloatValue
让animationOptions=(用户信息。[UIKeyboard.AnimationCurveUserInfo]:?>NSNumber).UITValue
让window=UIApplication.SharedApplication.KeyWindow
如果窗口为空,则
如果键盘正在显示,那么
控制台写入线(“已完成”)
设y=窗。框。高
Animate(新的时间间隔(animationDuration)、uiviewmanimationoptions(animationOptions)、fun()->
底视图。框架
控制台写入线(“已完成”)
)
))
您只是缺少lambda的参数部分:
fun () -> <function body goes here>
更新
很难说,因为我无法重现您的环境,但我已尝试用我的最佳猜测重新格式化您的代码示例:
UIView.Animate(
0,
0, // you might be missing an argument here?
UIViewAnimationOptions.CurveEaseOut,
(fun () -> x.View.LayoutIfNeeded()),
(fun () ->
let height : nfloat = Conversions.nfloat(220)
let animationDuration = (userInfo.[UIKeyboard.AnimationDurationUserInfoKey] :?> NSNumber).FloatValue
let animationOptions = (userInfo.[UIKeyboard.AnimationCurveUserInfoKey] :?> NSNumber).UintValue
let window = UIApplication.SharedApplication.KeyWindow
if window <> null and isKeyboardShowing then
Console.WriteLine("Completed")
let y = window.Frame.Height
UIView.Animate(
new TimeInterval(animationDuration),
UIViewAnimationOptions(animationOptions),
(fun () -> bottomView.Frame <- CGRect(0, y, bottomView.Frame.Width, bottomView.Frame.Height)),
(fun () -> Console.WriteLine("Completed")))))
UIView.Animate(
0,
0,//此处可能缺少参数?
UIViewAnimationOptions.CurveEaseOut,
(fun()->x.View.layoutifneed()),
(乐趣()->
let height:nfloat=Conversions.nfloat(220)
让animationDuration=(userInfo.[UIKeyboard.AnimationDurationUserInfoKey]:?>NSNumber).FloatValue
让animationOptions=(用户信息。[UIKeyboard.AnimationCurveUserInfo]:?>NSNumber).UITValue
让window=UIApplication.SharedApplication.KeyWindow
如果窗口为空且显示IsKeyboard,则
控制台写入线(“已完成”)
设y=窗。框。高
UIView。制作动画(
新时间间隔(animationDuration),
UIViewAnimationOptions(动画选项),
(fun()->bottomView.Frame Console.WriteLine(“已完成”;)))
这段代码变得越来越笨拙,因此我建议为回调定义单独的函数,这样应该更易于读取/格式化。此外,实际上似乎没有4参数版本的UIView.Animate
。您可能缺少持续时间
或延迟
参数?您只是缺少lambda的参数部分:
fun () -> <function body goes here>
更新
很难说,因为我无法重现您的环境,但我已尝试用我的最佳猜测重新格式化您的代码示例:
UIView.Animate(
0,
0, // you might be missing an argument here?
UIViewAnimationOptions.CurveEaseOut,
(fun () -> x.View.LayoutIfNeeded()),
(fun () ->
let height : nfloat = Conversions.nfloat(220)
let animationDuration = (userInfo.[UIKeyboard.AnimationDurationUserInfoKey] :?> NSNumber).FloatValue
let animationOptions = (userInfo.[UIKeyboard.AnimationCurveUserInfoKey] :?> NSNumber).UintValue
let window = UIApplication.SharedApplication.KeyWindow
if window <> null and isKeyboardShowing then
Console.WriteLine("Completed")
let y = window.Frame.Height
UIView.Animate(
new TimeInterval(animationDuration),
UIViewAnimationOptions(animationOptions),
(fun () -> bottomView.Frame <- CGRect(0, y, bottomView.Frame.Width, bottomView.Frame.Height)),
(fun () -> Console.WriteLine("Completed")))))
UIView.Animate(
0,
0,//此处可能缺少参数?
UIViewAnimationOptions.CurveEaseOut,
(fun()->x.View.layoutifneed()),
(乐趣()->
let height:nfloat=Conversions.nfloat(220)
让animationDuration=(userInfo.[UIKeyboard.AnimationDurationUserInfoKey]:?>NSNumber).FloatValue
让animationOptions=(用户信息。[UIKeyboard.AnimationCurveUserInfo]:?>NSNumber).UITValue
让window=UIApplication.SharedApplication.KeyWindow
如果窗口为空且显示IsKeyboard,则
控制台写入线(“已完成”)
设y=窗。框。高
UIView。制作动画(
新时间间隔(animationDuration),
UIViewAnimationOptions(动画选项),
(fun()->bottomView.Frame Console.WriteLine(“已完成”;)))
这段代码变得越来越笨拙,因此我建议为回调定义单独的函数,这样应该更易于读取/格式化。此外,实际上似乎没有4参数版本的UIView.Animate
。您可能缺少有关逗号问题的持续时间
或延迟
参数?:
在F#语法中,逗号并不表示lambda表达式的结尾,因此以下表达式:
fun x -> a, fun y -> b
将被解析为:
fun x -> (a, fun y -> b)
也就是说,它不会被理解为两个用逗号分隔的lambda,而是一个返回元组的lambda
要使其成为两个lambda,请使用括号:
(fun x -> a), (fun y -> b)
关于逗号问题:
在F#语法中,逗号并不表示lambda表达式的结尾,因此以下表达式:
fun x -> a, fun y -> b
将被解析为:
fun x -> (a, fun y -> b)
也就是说,它不会被理解为两个用逗号分隔的lambda,而是一个返回元组的lambda
要使其成为两个lambda,请使用括号:
(fun x -> a), (fun y -> b)
所以我不需要花括号?不,F#不使用花括号来包围函数体。通常使用空格/缩进表示范围,必要时可以将lambda括在括号中。如果希望第二个lambda不这样做,我该怎么办anything@Alk尝试传递ignore
以代替第二个lambdaignore
是一个函数,它接受一个参数(unit
,在您的例子中),不执行任何操作,也不返回任何内容/unit
@Alk再看看,您可能缺少了UIView.Animate
,或者您真的想使用另一个重载?看起来,如果要指定uiviewmanimationoptions
,则需要使用带有延迟的5参数重载