简体   繁体   English

如何将参数传递给NSAction?

[英]How to pass arguments to NSAction?

Well, everyone knows that in ObjC we have 好吧,每个人都知道在ObjC我们有

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

Notice that completion block has a BOOL argument. 请注意, completion块具有BOOL参数。 Now let's look at Monotouch: 现在让我们来看看Monotouch:

public static void Animate (double duration, double delay, UIViewAnimationOptions options, NSAction animation, NSAction completion)

NSAction is: NSAction是:

public delegate void NSAction ();

Just the delegate without any arguments. 只是没有任何争论的代表。 Moreover, in Monotouch "internals" we can see: 此外,在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 ();
        }
    });
}

Notice delegate (bool x) , It calls the function just like I need. 注意delegate (bool x) ,它就像我需要的那样调用函数。 Now, how can I pass Action<bool> as completion to UIView.Animate ? 现在,我如何将Action<bool>作为完成传递给UIView.Animate

That was an old binding bug (wrong type) and, for compatibility reason, Animate still use a NSAction completion handler. 这是一个旧的绑定错误(错误的类型),并且出于兼容性原因, Animate仍然使用NSAction完成处理程序。

To fix this a new method AnimateNotify was added to MonoTouch. 为了解决这个AnimateNotify ,在MonoTouch中添加了一个新方法AnimateNotify This version accept a UICompletionHandler which is defined like this: 此版本接受UICompletionHandler ,其定义如下:

public delegate void UICompletionHandler (bool finished);

So the solution to your problem is to use the newer AnimateNotify API. 因此,您的问题的解决方案是使用较新的AnimateNotify API。

So that should look like: 所以这应该是这样的:

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

}, delegate (bool finished) {

});

Or with lambda syntax: 或者使用lambda语法:

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

}, (finished) => {

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM