Articles in this section
Category / Section

How to render images within the application in carousel in different platforms

2 mins read

In Carousel, images take a major role. In Carousel needs to be able to share images across all platforms. These carousel’s images need to be configured based on the platform basis. Source of carousel’s images from any one of the following types

  1. Loading from file - Requires while filepath that can be resolved on each platform. (For Example: “/storage/emulated/0/Pictures/download1.JPG”)

C#

public partial class KBSolutionPage : ContentPage
{
public KBSolutionPage()
{
    InitializeComponent();
 
    SfCarousel sfCarousel = new SfCarousel();
 
    if (Device.OS == TargetPlatform.Android)
    {
        sfCarousel.ItemWidth = 170;
        sfCarousel.ItemHeight = 250;
    }
    if (Device.OS == TargetPlatform.iOS)
    {
        sfCarousel.ItemWidth = 150;
        sfCarousel.ItemHeight = 300;
    }
 
    var carouselModel = new List<CarouselModel> {
            new CarouselModel(“/storage/emulated/0/Pictures/download1.JPG" ),
 
            new CarouselModel(“/storage/emulated/0/Pictures/download2.JPG" ),
 
            new CarouselModel(“/storage/emulated/0/Pictures/download3.JPG" ),
 
            new CarouselModel(“/storage/emulated/0/Pictures/download4.JPG")
        };
    var carouselModelDataTemplate = new DataTemplate(() =>
    {
        var grid = new Grid();
        var nameLabel = new Image();
        nameLabel.SetBinding(Image.SourceProperty, "Image");
        grid.Children.Add(nameLabel);
        return grid;
    });
 
    sfCarousel.ItemTemplate = carouselModelDataTemplate;
    sfCarousel.DataSource = carouselModel;
 
    this.Content = sfCarousel;
}
 
}
public class CarouselModel
{
    public CarouselModel(string imagestr)
    {
        Image = imagestr;
    }
    private string _image;
 
    public string Image
    {
        get { return _image; }
        set { _image = value; }
    }
}

 

  1. Loading from URL images – Requires Url link (For Example: https://www.xamarin.com/content/images/pages/branding/assets/xamagon.png )

C#

public partial class KBSolutionPage : ContentPage
{
public KBSolutionPage()
{
    InitializeComponent();
 
    SfCarousel sfCarousel = new SfCarousel();
 
    if (Device.OS == TargetPlatform.Android)
    {
        sfCarousel.ItemWidth = 170;
        sfCarousel.ItemHeight = 250;
    }
    if (Device.OS == TargetPlatform.iOS)
    {
        sfCarousel.ItemWidth = 150;
        sfCarousel.ItemHeight = 300;
    }
 
    var carouselModel = new List<CarouselModel> {
        new CarouselModel ("https://cdn.syncfusion.com/content/images/Images/Camtasia_Succinctly.png?v=22022017060923"),
        new CarouselModel ("https://cdn.syncfusion.com/content/images/Images/SQL_Queries_Succinctly.png?v=04022017014551"),
        new CarouselModel ("https://cdn.syncfusion.com/content/images/Images/Keystonejs_Succinctly.png?v=22022017060923"),
        new CarouselModel ("https://cdn.syncfusion.com/content/images/Images/sql_server_for_c_sharp_developers_succinctly_cover_img.png?v=22022017060923"),
        new CarouselModel ("https://cdn.syncfusion.com/content/images/downloads/ebooks/SciPy_Programming_Succinctly_img.png?v=22022017060923")
 
    };
    var carouselModelDataTemplate = new DataTemplate(() =>
    {
        var grid = new Grid();
        var nameLabel = new Image();
        nameLabel.SetBinding(Image.SourceProperty, "Image");
        grid.Children.Add(nameLabel);
        return grid;
    });
 
    sfCarousel.ItemTemplate = carouselModelDataTemplate;
    sfCarousel.DataSource = carouselModel;
 
    this.Content = sfCarousel;
}
}
public class CarouselModel
{
public CarouselModel(string imagestr)
{
    Image = imagestr;
}
private string _image;
 
public string Image
{
    get { return _image; }
    set { _image = value; }
}
}
 

 

  1. Loading local images – Requires a resource identifier to an image file

C#

public partial class KBSolutionPage : ContentPage
{
public KBSolutionPage()
{
    InitializeComponent();
 
    SfCarousel sfCarousel = new SfCarousel();
 
    sfCarousel.ItemWidth = 170;
    sfCarousel.ItemHeight = 250;
 
    var carouselModel = new List<CarouselModel> {
        new CarouselModel ("image1.png"),
        new CarouselModel ("image2.png"),
        new CarouselModel ("image3.png"),
        new CarouselModel ("image4.png"),
        new CarouselModel ("image5.png")
    };
    var carouselModel DataTemplate = new DataTemplate(() =>
    {
        var grid = new Grid();
        var nameLabel = new Image();
        nameLabel.SetBinding(Image.SourceProperty, "Image");
        grid.Children.Add(nameLabel);
        return grid;
    });
 
    sfCarousel.ItemTemplate = carouselModel DataTemplate;
    sfCarousel.DataSource = carouselModel;
 
    this.Content = sfCarousel;
}
}
 
public class CarouselModel
{
public CarouselModel(string imagestr)
{
    Image = imagestr;
}
private string _image;
 
public string Image
{
    get { return _image; }
    set { _image = value; }
}
}

 

While accessing the images from local resource folder, need to ensure that their build action as per the platforms.

 

Platforms

Build Action

PCL

Embedded Resource

Android (Resource folder)

Android Resource

iOS (Resource folder)

Bundle Resource

UWP (Assets folder)

Content

 

 

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