【问题标题】:Identifying Dynamically created ImageButton click and its properties in Xamarin.forms在 Xamarin.forms 中识别动态创建的 ImageButton 单击及其属性
【发布时间】:2014-09-05 04:35:23
【问题描述】:

我已使用 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 );

            }
        }

提供有用的链接或示例代码将非常有帮助.. 在此先感谢..

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    如果您分享您的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 添加该项目

    【讨论】:

    • 我在 Xamarin.Forms.Labs 中使用了 ImageButton,将尝试使用 Command 属性并重新使用它。
    • 如何使用命令属性更改 ImageButtonSource 属性??
    • 感谢您的回答,您能帮我解决这个问题吗?让我们说,当我单击 ImageButton 时,我使用命令属性将 Icon1.png 更改为 Icon2.png .. 在第二次单击同一个 ImageButton 按钮时,我怎么能将其从 Icon2.png 改回 Icon1.png
    • 这里有一个提示:你也可以从命令处理程序内部更改 CommandParameter
    • 一个小小的说明。 Letz 说我有 5 个动态创建的按钮,当我单击 ImageButton 时,我需要更改所有 Imagebutton 的 Source 属性。有没有办法识别具有唯一 ID 的视图集。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2022-01-16
    相关资源
    最近更新 更多