Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the completion handler look like in C# when trying to animate?

I want translate this code

[UIView animateWithDuration:0.25
    animations:^{
        self.datePicker.alpha = 0.0f;
    }
    completion:^(BOOL finished){
        self.datePicker.hidden = YES;
    }
];

to Xamarin iOS:

UIView.Animate (0.25,
    animation: () => {
        this.datePicker.Alpha = 0.0f;
    },
    completion: (finished){
        this.datePicker.Hidden = true;
    }
);

The problem is in the completion block. How do I use the bool finished here?

I get

Unexpected Symbol {

like image 839
testing Avatar asked Sep 18 '14 09:09

testing


People also ask

What is a completion handler?

A completion handler in Swift is a function that calls back when a task completes. This is why it is also called a callback function. A callback function is passed as an argument into another function. When this function completes running a task, it executes the callback function.

What is completion block in Objective C?

A completion handler is nothing more than a simple block declaration passed as a parameter to a method that needs to make a callback at a later time.

What's the difference between Block and completion handler?

In short : Completion handlers are a way of implementing callback functionality using blocks or closures. Blocks and Closures are chunks of code that can be passed around to methods or functions as if they were values (in other words "anonymous functions" which we can give names to and pass around). Save this answer.

What is completion block?

The completion block should be used to notify interested objects that the work is complete or perform other tasks that might be related to, but not part of, the operation's actual task. A finished operation may finish either because it was cancelled or because it successfully completed its task.


2 Answers

It's basic lambda expression.

UIView.Animate (0.25,
    animation: () => {
        this.datePicker.Alpha = 0.0f;
    },
    completion: () => {
        this.datePicker.Hidden = true;
    }
);

Or since you have only one statement in your body, you can cut it down even further to

UIView.Animate (0.25,
    animation: () => this.datePicker.Alpha = 0.0f,
    completion: () => this.datePicker.Hidden = true
);
like image 198
Allen Zeng Avatar answered Oct 13 '22 00:10

Allen Zeng


Use UIView.AnimateNotify() to get the delegate for the completion handler that uses the bool parameter.

like image 44
jgoldberger - MSFT Avatar answered Oct 13 '22 00:10

jgoldberger - MSFT