I'm developing an Android App using Xamarin Android. Implementing the UI, I've a loop creating some buttons and it's handlers. My problem is thar whenever the handler is executed, the primitive value passed into the method is the reference of the outter value (i).
private void MyUiMethod(){
var i = 0;
foreach(var item in itemList){
var data = new MyCustomData();
var button = new Button();
button.Click = (s, e) => ButtonClick(s, e, data, i);
i++;
}
}
private void ButtClick(object s, EventArgs e, MyCustomData data, int index){
// HERE ALWAYS THE VALUE OF INDEX IS THE LAST VALUE
// e.g. If the list has 3 items, index = 3 no matter the button I've clicked;
}
Any suggestions?