Windows Phone 8.1 按钮点击参数

3

在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教程库,那也会很有帮助。

4个回答

3
您可以使用按钮的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>

这将产生以下输出: Output 在您的代码后台,您现在可以按以下方式使用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;
    }
}

1
这正是我所需要的。谢谢。而且,当我初始化页面时,您还可以通过编程方式为其分配标签,因此我甚至不必为每个按钮单独编写<Button Tag="1"/>。 - Velja
1
是的,你说得对... 那时候我没有想到... 好建议... :) - Akshay Soam

2

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


1
一个简单的解决方案是给每个按钮分配唯一的数字,然后调用函数时将这个唯一的数字作为参数传入。
在函数中,您可以轻松使用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
}

我希望这可以让它变得更加简单和高效。

我说我不想为每个按钮都制作一个处理函数,因为对于大网格来说这太难读了。 标签属性正是我所需要的。 - Velja

1
虽然答案向您展示了如何使用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
        }
    }
}

这种方法不需要您使用标签!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接