0

我使用 Xamrin.forms.labs 在共享代码中创建 Imagebutton。图像按钮的数量根据列表中元素的数量而有所不同。我的问题是

  1. 如何识别单击了哪个按钮?(我需要 Imagebutton 的文本进行识别)
  2. 我必须在第一次单击图像按钮时将图像更改为 Source_on.png,并在第二次单击时更改回 Source.png。(就像选择和取消选择一样)

我怎样才能实现它?

下面给出了我用来创建 ImageButtons 的代码。

            StackLayout Holder = new StackLayout {
            HorizontalOptions=LayoutOptions.FillAndExpand,
            VerticalOptions=LayoutOptions.Center,
            Orientation=StackOrientation.Horizontal,
            Spacing=2,
            };

        foreach (var options in list)
        {

                var Icon = new ImageButton () {
                    Source=Source.png,
                    BackgroundColor=Xamarin.Forms.Color.Transparent,
                    HorizontalOptions=LayoutOptions.CenterAndExpand,
                    VerticalOptions=LayoutOptions.CenterAndExpand,
                    Orientation=Xamarin.Forms.Labs.Enums.ImageOrientation.ImageOnTop,
                    Text=labeltxt,
                };

                Icon.Clicked += OnSelected;

                Holder.Children.Add (Icon );

            }
        }

提供有用的链接或示例代码将非常有帮助.. 提前致谢..

4

1 回答 1

0

如果您分享您的ImageButton.

话虽如此...如果您的 ImageButton 具有 Command 属性,它也将具有 CommandParameter 属性。如果它没有那个 - 制作它们。

您可以使用一些 ID 设置 CommandParameter 属性,然后将 Command 绑定到相同的代码,例如

ImageButton icon = null;
icon = new ImageButton{
...
  Command= new Command((tag)=>{
     icon.Source = ImageSource.FromFile("...");// see other .FromXYZ methods too
  },
  CommandParameter="<your tag here>",
}

但是......在你做所有这些检查Xamarin.Forms.Labs 之前,已经有一个 ImageButton 并且你可以通过在包管理器中搜索“Xamarin Forms Labs”从 NuGet 添加该项目

于 2014-07-15T05:42:50.583 回答