C# 如何在i';我通过循环将其添加到事件中

C# 如何在i';我通过循环将其添加到事件中,c#,list,events,xamarin,C#,List,Events,Xamarin,我正在创建一个应用程序,用户将在其中为这个按钮定义大量的按钮和命令。所以我有一个循环,我在那里设置了一个“点击”事件,但是当我尝试使用任何按钮时,我总是引用数组的最后一个元素。如何为每个按钮获取正确的命令 namespace TestButton { [Activity(Label = "TestButton", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activ

我正在创建一个应用程序,用户将在其中为这个按钮定义大量的按钮和命令。所以我有一个循环,我在那里设置了一个“点击”事件,但是当我尝试使用任何按钮时,我总是引用数组的最后一个元素。如何为每个按钮获取正确的命令

    namespace TestButton
{
    [Activity(Label = "TestButton", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            RelativeLayout ly = FindViewById<RelativeLayout>(Resource.Id.RelativeLayout);
            ObservableCollection<Button> button = new ObservableCollection<Button>();

            int _x = 0, _y = 0;
            int elements = 7;
            string[] commands = new string[] { "1", "2", "3", "4", "5", "6", "7" };

            for (int i = 0; i < 10; i++)
            {
                if (_x != 0)
                    if (_x % 3 == 0)
                    {
                        _y++;
                        _x = 0;
                    }
                button.Add(new Button(this));
                switch (_x)
                {
                    case 0:
                        button[i].SetX((720 / 2 - 150 / 2) - (20 + 150));
                        break;
                    case 1:
                        button[i].SetX(720 / 2 - 150 / 2);
                        break;
                    case 2:
                        button[i].SetX((720 / 2 - 150 / 2) + (20 + 150));
                        break;
                    default:
                        break;
                }

                button[i].SetHeight(150);
                button[i].SetMinimumWidth(10);
                button[i].SetWidth(150);
                button[i].SetY(20 + (_y * 150) + (20 * 1 * _y));
                button[i].SetText("1", TextView.BufferType.Normal);
                button[i].SetBackgroundResource(Resource.Drawable.RoundButtonEnabled);
                button[i].Click += (object sender, EventArgs e) =>
                {
                    WriteCommand(commands[i]); //How can I set the "i" command for the button?
                };
                ly.AddView(button[i]);

                elements--;
                if (elements <= 0)
                    break;

                _x++;
            }

        }

        private void WriteCommand(string command)
        {
            Console.WriteLine(command);
        }
    }
}
名称空间测试按钮
{
[活动(Label=“TestButton”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
RelativeLayout ly=FindViewById(Resource.Id.RelativeLayout);
ObservableCollection按钮=新建ObservableCollection();
int x=0,y=0;
int元素=7;
字符串[]命令=新字符串[]{“1”、“2”、“3”、“4”、“5”、“6”、“7”};
对于(int i=0;i<10;i++)
{
如果(_x!=0)
如果(_x%3==0)
{
_y++;
_x=0;
}
按钮。添加(新按钮(此));
开关(_x)
{
案例0:
按钮[i].SetX((720/2-150/2)-(20+150));
打破
案例1:
按钮[i].SetX(720/2-150/2);
打破
案例2:
按钮[i].SetX((720/2-150/2)+(20+150));
打破
违约:
打破
}
按钮[i]。设置高度(150);
按钮[i]。设置最小宽度(10);
按钮[i]。设置宽度(150);
按钮[i].SetY(20+(y*150)+(20*1*y));
按钮[i].SetText(“1”,TextView.BufferType.Normal);
按钮[i].SetBackgroundResource(Resource.Drawable.RoundButtonEnabled);
按钮[i]。单击+=(对象发送者,事件参数e)=>
{
WriteCommand(commands[i]);//如何设置按钮的“i”命令?
};
ly.AddView(按钮[i]);
元素--;

if(在
for
循环中的元素执行
var i_local=i;
并在事件处理程序中使用
i_local
。好吧,谢谢!这项工作很好!在创建局部var之前我已经尝试过了,但我是在事件处理程序中执行的。我的错:)再次感谢!