Binding a method for Block in Xamarin

人走茶凉 提交于 2019-12-22 17:06:54

问题


I am trying to add a binding for this in Xamarin iOS. How can I convert that in 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);
    }
  }
}

Thanks.


回答1:


I assume that you are creating iOS binding library.

First, You must declare a delegate that match with the block

// 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);

To invoke it, you can use methods, or lambdas:

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

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

Or with lambdas:

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


来源:https://stackoverflow.com/questions/34237274/binding-a-method-for-block-in-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!