Articles in this section
Category / Section

How to achieve item tapped in Xamarin.Forms SfRotator?

1 min read

Using TapGestureRecognizer, you can make Xamarin.Forms SfRotator items clickable. Create an instance of GestureRecognizers, and set the Tapped event to Xamarin.Forms SfRotator items.

 

The following steps explain how to achieve item tapped in Xamarin.Forms SfRotator.

 

Step 1: Create an app using Xamarin.Forms SfRotator control with all the required assemblies.

 

Step 2: Create a simple view model class, and add the items that need to be displayed in Xamarin.Forms SfRotator. The following code sample demonstrates this.

 

Model

 

public class RotatorModel
{
    public RotatorModel(string image)
    {
        Image = image;
    }
    private string _image;
    public string Image
    {
        get { return _image; }
        set { _image = value; }
    }
}

 

ViewModel

 

public class RotatorViewModel
{
    public RotatorViewModel()
    {
        ImageCollection.Add(new RotatorModel("image1.png"));
        ImageCollection.Add(new RotatorModel("image2.png"));
        ImageCollection.Add(new RotatorModel("image3.png"));
        ImageCollection.Add(new RotatorModel("image4.png"));
        ImageCollection.Add(new RotatorModel("image5.png"));
    }
    private List<RotatorModel> imageCollection = new List<RotatorModel>();
    public List<RotatorModel> ImageCollection
    {
        get { return imageCollection; }
        set { imageCollection = value; }
    }
}

 

Step 3: Initialize the Xamarin.Forms SfRotator in XAML page in which the view model class is set to BindingContext, and use GestureRecognizer to make the Xamarin.Forms SfRotator items clickable.

 

<rotator:SfRotator>
    <rotator:SfRotator.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding Image}">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"
                                          NumberOfTapsRequired="1">
                    </TapGestureRecognizer>
                </Image.GestureRecognizers>
            </Image>
        </DataTemplate>
    </rotator:SfRotator.ItemTemplate>
</rotator:SfRotator>

 

Output:

 

Item tapped in Xamarin.Forms SfRotator.

 

You can find the sample in the following link: Sample

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied