Xamarin.Forms - CollectionView

Introduction

 
Xamarin.Forms - CollectionView
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem.
 
This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 

CollectionView

 
CollectionView is introduced in Xamarin.Forms 4.0 pre-releases. CollectionView is a view for presenting lists of data using different layout specifications. It aims to provide a more flexible, and performant alternative to ListView.
  • CollectionView supports single and multiple selections.
  • CollectionView has a flexible layout model, which allows data to be presented vertically or horizontally.
Note
CollectionView is only available on iOS and Android.
 
More information about Collection View
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.
 
Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.
 
Now, you need to click "Create a new project".
 
Xamarin.Forms - CollectionView
 
Now, filter by Project Type: Mobile
 
Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
 
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
 
Now, select the blank app and target platforms - Android, iOS and Windows (UWP).
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 
Note
Before you initialize Xamarin.Forms in your MainActivity.cs and AppDelegate, add the following flag.
  1. Forms.SetFlags("CollectionView_Experimental");  
Android Implementation
 
MainActivity.cs
  1. protected override void OnCreate(Bundle savedInstanceState)  
  2.         {  
  3.             TabLayoutResource = Resource.Layout.Tabbar;  
  4.             ToolbarResource = Resource.Layout.Toolbar;  
  5.   
  6.             base.OnCreate(savedInstanceState);  
  7.   
  8.             global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");  
  9.             Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
  10.             global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
  11.             LoadApplication(new App());  
  12.         }  
iOS Implementation

AppDelegate.cs
  1. public override bool FinishedLaunching(UIApplication app, NSDictionary options)  
  2.         {  
  3.             global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");  
  4.             global::Xamarin.Forms.Forms.Init();  
  5.             LoadApplication(new App());  
  6.   
  7.             return base.FinishedLaunching(app, options);  
  8.         }  

Setting up the User Interface

 
Now, implement the CollectionView with data binding.
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"    
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.              mc:Ignorable="d"    
  7.              x:Class="CollectionViewChallenge.Views.CollectionViewChallengePage">    
  8.     <ContentPage.Content>    
  9.         <StackLayout BackgroundColor="White">    
  10.             <!-- Use your own layout and functionality here! -->    
  11.             <CollectionView x:Name="collectionList" Margin="10">    
  12.                 <CollectionView.ItemTemplate>    
  13.                     <DataTemplate>    
  14.                         <Grid Padding="10" Margin="10">    
  15.                             <Grid.RowDefinitions>    
  16.                                 <RowDefinition Height="Auto" />    
  17.                                 <RowDefinition Height="Auto" />    
  18.                             </Grid.RowDefinitions>    
  19.                             <Grid.ColumnDefinitions>    
  20.                                 <ColumnDefinition Width="Auto" />    
  21.                                 <ColumnDefinition Width="Auto" />    
  22.                                 <ColumnDefinition Width="Auto" />    
  23.                             </Grid.ColumnDefinitions>    
  24.                             <Frame BackgroundColor="White" Grid.RowSpan="2" CornerRadius="5" Padding="10">    
  25.                                 <Image    
  26.                        Source="iphone6.jpeg"    
  27.                        Aspect="AspectFill"    
  28.                        HeightRequest="100"    
  29.                        WidthRequest="100" />    
  30.                             </Frame>    
  31.                             <Label Grid.Column="1"    
  32.                        Text="{Binding Title}"    
  33.                        FontAttributes="Bold" />    
  34.                             <Label Margin="0,20,0,0" Grid.Column="1"    
  35.                        Text="{Binding Description}"    
  36.                        FontAttributes="Bold" />    
  37.                             <Label Grid.Row="1"    
  38.                        Grid.Column="1"    
  39.                        Text="{Binding Price}"    
  40.                        VerticalOptions="End" />    
  41.                             <Frame Grid.Row="0"    
  42.                        Grid.Column="2" BackgroundColor="Pink" Padding="10">    
  43.                                 <Label TextColor="White"     
  44.                        Text="{Binding Percentage}"    
  45.                        VerticalOptions="Start" />    
  46.                             </Frame>    
  47.                                 
  48.                             <Image Grid.Row="1" Grid.Column="2"    
  49.                        Source="star.png"    
  50.                        HeightRequest="5"    
  51.                        WidthRequest="5" />    
  52.                         </Grid>    
  53.                     </DataTemplate>    
  54.                 </CollectionView.ItemTemplate>    
  55.     
  56.             </CollectionView>    
  57.         </StackLayout>    
  58.     </ContentPage.Content>    
  59. </ContentPage>    
CollectionView ItemSource
 
Now, let us add the ItemSource to the CollectionView.
  1. using Xamarin.Forms;  
  2. using Xamarin.Forms.Xaml;  
  3.   
  4. namespace CollectionViewChallenge.Views  
  5. {  
  6.     [XamlCompilation(XamlCompilationOptions.Compile)]  
  7.     public partial class CollectionViewChallengePage : ContentPage  
  8.     {  
  9.         public CollectionViewChallengePage()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.         protected override void OnAppearing()  
  14.         {  
  15.             base.OnAppearing();  
  16.   
  17.             List<ListItem> listItems = new List<ListItem>();  
  18.             ListItem listItem = new ListItem() {Title="iPhone",Description="2GB RAM, 16GB Internal", Price="$500" ,Percentage="50%"};  
  19.             ListItem listItem1 = new ListItem() { Title = "iPhone", Description = "2GB RAM, 16GB Internal", Price = "$500", Percentage = "50%" };  
  20.             ListItem listItem2 = new ListItem() { Title = "iPhone", Description = "2GB RAM, 16GB Internal", Price = "$500", Percentage = "50%" };  
  21.             ListItem listItem3 = new ListItem() { Title = "iPhone", Description = "2GB RAM, 16GB Internal", Price = "$500", Percentage = "50%" };  
  22.             listItems.Add(listItem);  
  23.             listItems.Add(listItem1);  
  24.             listItems.Add(listItem2);  
  25.             listItems.Add(listItem3);  
  26.             collectionList.ItemsSource = listItems;  
  27.         }  
  28.     }  
  29. }  
Click the "Play" button to try it out.
 
Xamarin.Forms - CollectionView
 
I hope you have understood how to use CollectionView instead of ListView in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)


Similar Articles