Xamarin Guide 4: Create the SessionDetailsView

Scope

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

Before reading this article you must read:

Guide 4. Create the SessionDetailsView

In the last step, you created the menu for each Session and it created the “Details” option that has the goal to show the details from a selected session.

Let's create the SessionDetailsView!

Create a new XAML page, like you did to create the SessionsView page. Then open the SessionDetailsView and you should have something as in the following:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage  
  3.     xmlns="http://xamarin.com/schemas/2014/forms"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"      
  5. x:Class="ENEI.SessionsApp.Views.SessionDetailsView">  
  6. </ContentPage>    

Then create the content, as in the following:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.   
  3. <ContentPage x:Class="ENEI.SessionsApp.View.SessionDetailsView" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" BackgroundColor="White" Icon="ic_action_users.png">  
  4.     <StackLayout BackgroundColor="White" Spacing="20">  
  5.         <StackLayout Orientation="Horizontal" Padding="20,10,0,0">  
  6.             <StackLayout.IsVisible>  
  7.                 <OnPlatform Android="false"  WinPhone="true" iOS="false” x:TypeArguments="x:Boolean" />  
  8.             </StackLayout.IsVisible>  
  9.             <Image WidthRequest="48" HeightRequest="38" Source="Images/ic_action_users.png"/>  
  10.             <Label FontSize="Large" FontAttributes="Bold" Text="{Binding Name}" TextColor="Black"/>  
  11.         </StackLayout>  
  12.         <StackLayout Orientation="Horizontal" Padding="20,20,20,0" Spacing="20">  
  13.             <Image HorizontalOptions="Start" Source="{Binding Speaker.ImageUrl}" VerticalOptions="Start">  
  14.                 <Image.WidthRequest>  
  15.                     <OnPlatform Android="50" WinPhone="200" iOS="50" x:TypeArguments="x:Double" />  
  16.                 </Image.WidthRequest>  
  17.                 <Image.HeightRequest>  
  18.                     <OnPlatform Android="50" WinPhone="200" iOS="50" x:TypeArguments="x:Double" />  
  19.                 </Image.HeightRequest>  
  20.             </Image>  
  21.             <StackLayout HorizontalOptions="Start" Padding="10,0,0,0">  
  22.                 <Label Font="Large" HorizontalOptions="Start" LineBreakMode="WordWrap" Text=" {Binding Speaker.Name}" TextColor="Black" />  
  23.                 <Label Font="Small" HorizontalOptions="Start" LineBreakMode="TailTruncation" Text="{Binding Date}" TextColor="Black" />  
  24.             </StackLayout>  
  25.         </StackLayout>  
  26.         <ScrollView VerticalOptions="FillAndExpand" Padding="20,20,20,0" 
  27.             <Label Font="Medium" HorizontalOptions="Start" LineBreakMode="WordWrap" Text="{Binding Description}"  TextColor="Black" />  
  28.         </ScrollView>  
  29.     </StackLayout>  
  30. </ContentPage>   

And in the code behind define the BindingContext and the Title, as in the following: 

  1. public partial class SessionDetailsView : ContentPage    
  2. {   
  3.    private readonly Session _session;    
  4.    public SessionDetailsView(Session session)    
  5.    {      
  6.       _session = session;    
  7.       InitializeComponent();    
  8.       Title = session.Name;    
  9.       BindingContext = session;    
  10.    }    
  11. }    

This page will have the BindingContext defined with the session object, but could be the page as in SessionsView.

The Details gesture

Now that you have the new page created, you need to connect both of the pages and it is possible through the “Detail” option for each session in ListView. This way, in the SessionsView we need to use the details gesture to navigate from the SessionsView to the SessionDetailsView, sending the selected session. The implementation can be something as in the following:

  1. private void DetailsGesture_OnTapped(object sender, EventArgs e)     
  2. {    
  3.    var tappedEventArg = e as TappedEventArgs;    
  4.    if (tappedEventArg != null)    
  5.    {    
  6.        var session = tappedEventArg.Parameter as Session;    
  7.        if (session != null)    
  8.        {    
  9.            Navigation.PushAsync(new SessionDetailsView(session), true);    
  10.        }    
  11.    }    
  12. }    

Running the application

At this moment, you should navigate to the Session details view that the result should be as described in the following figure.


The navigation from SessionDetailsView is made using the back button provided in iOS and Android pages and by the physical back button from Windows Phone that does not require implementation.