如何向NSAction传递参数?

7

大家都知道,ObjC中我们有

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

请注意,completion块有一个BOOL参数。 现在让我们看看Monotouch:
public static void Animate (double duration, double delay, UIViewAnimationOptions options, NSAction animation, NSAction completion)

NSAction是一个:

public delegate void NSAction ();

只有委托而没有任何参数。此外,在Monotouch的“内部”中,我们可以看到:

public static void Animate (double duration, double delay, UIViewAnimationOptions options, 
NSAction animation, NSAction completion)
{
    UIView.AnimateNotify (duration, delay, options, animation, delegate (bool x)
    {
        if (completion != null)
        {
            completion ();
        }
    });
}

注意 delegate (bool x), 它会像我需要的那样调用函数。现在,我如何将 Action<bool> 作为完成处理程序传递给 UIView.Animate

2个回答

8

这是一个老的绑定错误(类型错误),为了兼容性,Animate 仍使用 NSAction 完成处理程序。

为了解决这个问题,MonoTouch 添加了一个新的方法 AnimateNotify。该版本接受一个定义如下的 UICompletionHandler

public delegate void UICompletionHandler (bool finished);

因此,解决您的问题的方法是使用较新的AnimateNotify API。

你不觉得是时候移除旧的封装器了吗? - Maxim Korobov
@Maxim:我想一夜之间将其删除可能有些过头了。但是让它过时对我来说听起来是个好主意。(我不是机器翻译开发人员,只是一个用户。) - Dan Abramov

5
那应该看起来像这样:

UIView.AnimateNotify(duration, 0, UIViewAnimationOptions.CurveEaseInOut, delegate () {

}, delegate (bool finished) {

});

或者使用lambda语法:

UIView.AnimateNotify(duration, 0, UIViewAnimationOptions.CurveEaseInOut, () => {

}, (finished) => {

});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接