简体   繁体   中英

Identifying Dynamically created ImageButton click and its properties in Xamarin.forms

I have used Xamrin.forms.labs to create Imagebutton in shared code. Number of imagebuttons varies according to the number of elements in the list. My questions are

  1. How can I identify which button is clicked? (I need the text of the Imagebutton for identification)
  2. I have to change the image to Source_on.png on the first click of the image button and change back to Source.png on second click. (Just like select and unselect)

How can I acheive it??

The code I have used to create ImageButtons is given below.

            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 );

            }
        }

Providing a helpful link or sample code will be really helpful.. Thanks in Advance..

It would help a lot if you share the code of your ImageButton .

That being said... if your ImageButton has a Command property it would also have a CommandParameter property. If it doesn't have that - make them.

You'd set the CommandParameter property with some ID and then bind the Command to the same code, such as

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

BUT... before you do all of that check out the Xamarin.Forms.Labs , there's an ImageButton there already and you can add that project from NuGet by searching for "Xamarin Forms Labs" in the package manager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM