Windows phone 8.1 按钮点击参数

Windows phone 8.1 button click arguments

在 Windows Phone 8.1 中有没有办法在按钮点击时传递参数?

我有一个 5x5 按钮网格,它们应该都调用相同的方法但使用不同的参数。我正在添加这样的处理程序:

foreach (var child in buttonGrid.Children)
{
    Button b = child as Button;
    if (b != null)
    {
        b.Click += Button_Click;
        // I want to add an argument to this
    }
}

现在我可以获得按钮索引的唯一方法是遍历整个网格并检查发送者是否等于按钮:

private void Button_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < buttonGrid.Children.Count; i++)
    {
        if (sender == buttonGrid.Children[i])
        {
            DoSomething(i);
            return;
        }
    }
}

它有效,但我不太喜欢这种方法。有没有更有效的方法(除了为 25 个按钮中的每一个创建不同的方法)?

我尝试在互联网上搜索,但是 Windows Phone 的文档和示例确实缺乏。如果有人有一个很好的 Windows Phone 8.1 教程库来指导我,那也会有帮助。

XAML 中的控件和其他元素有一个 Tag 属性,您可以为此类事物设置任意对象值。在创建对象时设置它,然后在事件处理程序中检查它。

可以使用Tag属性的按钮。

例如

我正在尝试创建一个数字键盘,它有 9 个按钮,各自的数字作为按钮内容,我设置了与 Tag 属性 相同的内容。

<StackPanel>
    <StackPanel Orientation="Horizontal" >
        <Button Content="1" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="1" />
        <Button Content="2" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="2" />
        <Button Content="3" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="3" />
    </StackPanel>

    <StackPanel Orientation="Horizontal">
        <Button Content="4" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="4" />
        <Button Content="5" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="5" />
        <Button Content="6" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="6" />
    </StackPanel>

    <StackPanel Orientation="Horizontal">
        <Button Content="7" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="7" />
        <Button Content="8" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="8" />
        <Button Content="9" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="9" />
    </StackPanel>
</StackPanel>

这会产生以下输出:

在您后面的代码中,您现在可以按以下方式使用 Tag 属性

private void Button_Click(object sender, RoutedEventArgs e)
{
    var tag = (sender as Button).Tag;

    int t = Convert.ToInt16(tag);

    switch (t)
    {
        case 1:

            //Do Something
            break;

        case 2:

            //Do Something
            break;

        case 3:

            //Do Something
            break;

        case 4:

            //Do Something
            break;

        case 5:

            //Do Something
            break;

        case 6:

            //Do Something
            break;

        case 7:

            //Do Something
            break;

        case 8:

            //Do Something
            break;

        case 9:

            //Do Something
            break;

        default:
            break;
    }
}

一个简单的解决方案是为每个按钮分配唯一编号,然后使用这个唯一分配的编号作为参数调用函数。

在函数中,您可以轻松使用 If-Else 块根据唯一编号执行任务。

4 个按钮的代码如下:-

void func(int unique_number)
{
    if(unique_number==1)
    {
        //perform tasks for button 1
    }

    if(unique_number==2)
    {
        //perform tasks for button 2
    }

    if(unique_number==3)
    {
        //perform tasks for button 3
    }

    if(unique_number==4)
    {
        //perform tasks for button 4
    }

}

private void Button_1_Click(object sender, RoutedEventArgs e)
{
   func(1); //Call from button 1
}

private void Button_2_Click(object sender, RoutedEventArgs e)
{
   func(2); //Call from button 2
}

private void Button_3_Click(object sender, RoutedEventArgs e)
{
   func(3); //Call from button 3
}

private void Button_4_Click(object sender, RoutedEventArgs e)
{
   func(4); //Call from button 4
}

我希望这能让它变得简单和高效。

虽然答案向您展示了如何使用 Tag 执行此操作,但这种方法不方便,例如 100 个按钮。

要实现您的结果,您可以使用闭包来完成这项工作。方法如下:

// When you're attaching handlers
int i=0;
foreach (var child in buttonGrid.Children)
{
    Button b = child as Button;
    if (b != null)
    {
        b.Click += () => {
            int z = i++; // You can put a different id generator here as well, like int z = <rand>%<prime> if you want
            DoSomething(z); // DoSomething called with corrosponding button id
        }
    }
}

此方法不需要您使用标签!