加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Windows phone 8.1按钮单击参数

发布时间:2020-12-16 02:04:28 所属栏目:百科 来源:网络整理
导读:有没有办法在 Windows Phone 8.1中按下按钮点击参数? 我有一个5×5按钮的网格,他们都应该调用相同的方法,但使用不同的参数.我正在添加这样的处理程序: foreach (var child in buttonGrid.Children){ Button b = child as Button; if (b != null) { b.Click
有没有办法在 Windows Phone 8.1中按下按钮点击参数?

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

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教程存储库来指导我,那也会有所帮助.

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读