7

我有一个客户列表,我用它来创建这样的元素:

Foreach(Customer c in Customers)
{
    //Make the StyledStringElement
    //Set the Tapped to action a touch
    element.Tapped += () => {  Push (new SomeController (c.ClientId)); };
}

问题是当元素被点击时,它会将最后一个客户发送到 SomeController ()。

如何使用可以识别客户的信息设置 Tapped Delegate ?

4

1 回答 1

13

您需要将客户保留为循环中的局部变量:

foreach(Customer c in Customers)
{    
    //Make the StyledStringElement
    //Set the Tapped to action a touch
    var currentCustomer = c;
    element.Tapped += () => {  Push (new SomeController (currentCustomer.ClientId)); };
}

但这不是 MonoTouch.Dialog 的限制。是一篇关于一般问题的文章。

于 2011-04-18T09:38:34.033 回答