Xamarin Guide 10: Move ItemTemplate to Resources

Scope

This Xamarin Workshop Guide was created for the The Portuguese National Meeting of IT Students (ENEI) by Sara Silva and the original content is available here. To extend it to the global community, it was published in a new project called Xam Community Workshop and the main goal is for any developer, or user group to customize it for their events.

Before reading this article you must read:

Guide 10. Move ItemTemplate to Resources

In the last two steps you learned how to create an App class using an XAML approach and how to use the MVVM pattern, to separate the UI from the logic. In this step you learn how extract the DataTemplate from the listview’s Itemtemplate to Resources.

In the App.xaml prepare the code to add the DataTemplate, for that you need to define the Application’s Resources as in the following:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Application xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="ENEI.SessionsApp.App">  
  5.   <Application.Resources>  
  6.     <ResourceDictionary>  
  7.        
  8.     </ResourceDictionary>  
  9.   </Application.Resources>  
  10. </Application>  
And then copy the DataTemplate defined inside the ListView’s ItemTemplate as in the following:
  1. <ListView x:Name="SessionsList"  
  2.                Grid.Row="1"  
  3.                RowHeight="{Binding Converter={StaticResource RowHeightConverter}}"  
  4.                ItemSelected="SessionsList_OnItemSelected"  
  5.                ItemsSource="{Binding Sessions}"  
  6.                SeparatorColor="#0094FF">  
  7.   <ListView.ItemTemplate>  
  8.     <DataTemplate>  
  9.         <!—this DataTemplate-->  
  10.     </DataTemplate>  
  11. </ListView.ItemTemplate>  
To inside the ResourceDictionary as in the following:
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Application xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="ENEI.SessionsApp.App">  
  5.   <Application.Resources>  
  6.     <ResourceDictionary>  
  7.       <DataTemplate>  
  8.         <ViewCell>  
  9.           <ViewCell.View>  
  10.             <Grid BackgroundColor="Transparent" Padding="20,0,20,0">  
  11.               <Grid.ColumnDefinitions>  
  12.                 <ColumnDefinition Width="Auto" />  
  13.                 <ColumnDefinition Width="*" />  
  14.               </Grid.ColumnDefinitions>  
  15.               <Grid.RowDefinitions>  
  16.                 <RowDefinition Height="5" />  
  17.                 <RowDefinition Height="Auto" />  
  18.                 <RowDefinition Height="48" />  
  19.                 <RowDefinition Height="5" />  
  20.               </Grid.RowDefinitions>  
  21.               <Image Grid.Row="1" HorizontalOptions="StartAndExpand"  
  22.                      Source="{Binding Speaker.ImageUrl}"  
  23.                      VerticalOptions="Start">  
  24.                 <Image.WidthRequest>  
  25.                   <OnPlatform Android="50"  
  26.                               WinPhone="100"  
  27.                               iOS="50"  
  28.                               x:TypeArguments="x:Double" />  
  29.                 </Image.WidthRequest>  
  30.                 <Image.HeightRequest>  
  31.                   <OnPlatform Android="50"  
  32.                               WinPhone="100"  
  33.                               iOS="50"  
  34.                               x:TypeArguments="x:Double" />  
  35.                 </Image.HeightRequest>  
  36.               </Image>  
  37.               <StackLayout Grid.Row="1"  
  38.                            Grid.Column="1"  
  39.                            HorizontalOptions="FillAndExpand"  
  40.                            Padding="10,0,0,0">  
  41.   
  42.                 <Label Font="Medium"  
  43.                        FontAttributes="Bold"  
  44.                        Text="{Binding Name}"  
  45.                        TextColor="Black" />  
  46.   
  47.                 <Label Font="Medium"  
  48.                        LineBreakMode="TailTruncation"  
  49.                        Text="{Binding Speaker.Name}"  
  50.                        TextColor="Black" />  
  51.                 <Label Font="Small"  
  52.                        LineBreakMode="TailTruncation"  
  53.                        TextColor="Black" Text="{Binding Details}"/>  
  54.               </StackLayout>    
  55. ….  
  56.   
  57.       </DataTemplate>  
  58.     </ResourceDictionary>  
  59.   </Application.Resources>  
  60. </Application>  
Then, define the x:key value to identify the DataTemplate as in the following:
  1. <Application.Resources>  
  2.     <ResourceDictionary>  
  3.       <DataTemplate x:Key="SessionTemplate">  
If you run the application you will get some errors because it is required to move the converters and the SessionViewModel to App.xaml as in the following:
  1. <Application.Resources>  
  2.     <ResourceDictionary>  
  3.       <viewModels:SessionViewModel x:Key="SessionViewModel"/>  
  4.       <converters:FavoriteImageConverter x:Key="FavoriteImageConverter"/>
  5.       <converters:ImageSizeConverter x:Key="ImageSizeConverter"/>  
  6.       <converters:ImageUrlConverter x:Key="ImageUrlConverter"/>  
  7.       <DataTemplate x:Key="SessionTemplate">  
  8.         <ViewCell>    
Now you can use the DataTemplate defined in Resources, using the key defined “SessionTemplate” as in the following:
  1. !-- ListView will be defined here -->  
  2.     <ListView x:Name="SessionsList"  
  3.                    Grid.Row="1"  
  4.                    RowHeight="{Binding Converter={StaticResource RowHeightConverter}}"  
  5.                    ItemSelected="SessionsList_OnItemSelected"  
  6.                    ItemsSource="{Binding Sessions}"  
  7.                    SeparatorColor="#0094FF" ItemTemplate="{StaticResource SessionTemplate}">  
This way, the SessionsView.xaml looks cleaner, it is easier to understand the code and is possible to reuse the DataTemplate if needed.

Up Next
    Ebook Download
    View all
    Learn
    View all