绑定Xamarin中Block的方法

时间:2015-12-12 07:05:10

标签: ios objective-c xamarin delegates xamarin.ios

我正在尝试在Xamarin iOS中为此添加绑定。我怎样才能在Xamarin中转换它?

(void)orderingTiles
{
  [self traverseTilesWithBlock:^(UIImageView *tileImageView, int i, int j) 
  {       
   [self bringSubviewToFront:tileImageView];
  }];

}


(void)traverseTilesWithBlock:(void (^)(UIImageView *tileImageView, int i, int j))block

{
  for (int j = 1; j <= self.board.size; j++) {

    for (int i = 1; i <= self.board.size; i++) {
        NSNumber *value = [self.board tileAtCoordinate:CGPointMake(i, j)];
        if ([value intValue] == 0) continue;
        UIImageView *tileImageView = [self.tiles objectAtIndex:[value intValue]-1];
        block(tileImageView, i, j);
    }
  }
}

感谢。

1 个答案:

答案 0 :(得分:1)

我假设你正在创建https://github.com/lzyzsd/CircleProgress

首先,您必须声明与块匹配的委托

// This declares the callback signature for the block:
delegate void TraverseBlock (UIImageView *tileImageView, int i, int j)

// Later, inside your definition, do this:
[Export ("traverseTilesWithBlock:")]
void TraverseTilesWithBlock (TraverseBlock block);

要调用它,您可以使用方法或lambdas:

foo.TraverseTilesWithBlock (MyTraverseFunc);
[...]

void MyTraverseFunc (UIImageView tileImageView, int i, int j)
{
    // do something
}

或者使用lambdas:

foo.TraverseTilesWithBlock ((tileImageView, i, j) => {
    // do something
});