Xamarin.Forms: Data Binding (Android, iOS and Windows)

Step 1

First create a Xamarin.Forms project with Portable Class Library (PCL) as discussed in my previous article, Xamarin.Forms: Create Your First Xamarin App For iOS, Android, And Windows

Step 2

Important: This article is based on my previous article. So go through my previous article Xamarin.Forms: Create A Simple LoginUI before following this article.

In the previous article, I have created a Simple Login UI using the XAML Content Page with the input field, data-picker, time-picker, buttons, etc.

Step 3

From the previous article, we have the UI like:



Step 4

Now add a XAML page in PCL project. Right click on PCL project > Add > New item > Cross Platform > Forms Xaml Page and Click Add.





Step 5

In the MainPage.xaml.cs, update OnView function with the following code snippet.

Note

This is from the previous article. So please go through the previous article first.

  1. publicasyncvoidOnView(object o, EventArgs e)  
  2. {  
  3.    //await DisplayAlert("Members", member[0].FirstName + "," + member[0].MiddleName + "," + member[0].LastName + "," + member[0].DateTime   , "Cancel");  
  4.    awaitNavigation.PushAsync(newTasksPage(member));  
  5. }  
Step 6

In the TasksPage.xaml, let's add a ListView inside ContentPage.Content. Inside the ListView, we have ItemTemplate and DataTemplate. We use ViewCell to display our content.

Complete XAML code will be
  1. <?xmlversion="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="Data_Entry.TasksPage">  
  6.     <ContentPage.Content>  
  7.         <StackLayoutPadding="30">  
  8.             <ListViewx:Name="MemberList"  
  9.             ItemTapped="OnSelected"  
  10.             ItemsSource="{Binding Members}">  
  11.                 <ListView.ItemTemplate>  
  12.                     <DataTemplate>  
  13.                         <ViewCell>  
  14.                             <ViewCell.View>  
  15.                                 <StackLayoutPadding="5"Spacing="2">  
  16.                                     <StackLayoutOrientation="Horizontal">  
  17.                                         <LabelText="First Name: "FontSize="1/>  
  18.                                         <Labelx:Name="firstName"  
  19.                                             Text="{Binding FirstName}"  
  20.                                             FontSize="16"  
  21.                                             TextColor="Red"/>  
  22.                                     </StackLayout>  
  23.                                     <StackLayoutOrientation="Horizontal">  
  24.                                         <LabelText="Middle Name: "FontSize"1/>  
  25.                                         <Labelx:Name="middleName"  
  26.                                         Text="{Binding MiddleName}"  
  27.                                         FontSize="16"  
  28.                                         TextColor="Red"/>  
  29.                                     </StackLayout>  
  30.                                     <StackLayoutOrientation="Horizontal">  
  31.                                         <LabelText="Last Name: "FontSize="16/>  
  32.                                         <Labelx:Name="lastName"  
  33.                                             Text="{Binding LastName}"  
  34.                                             FontSize="16"TextColor="Red"/>  
  35.                                     </StackLayout>  
  36.                                     <StackLayoutOrientation="Horizontal">  
  37.                                         <LabelText="DateTime: "FontSize="16"/>  
  38.                                         <Labelx:Name="dateTime"Text="{Binding                                             DateTime}"  
  39.                                             FontSize="16"  
  40.                                             TextColor="Red"/>  
  41.                                     </StackLayout>  
  42.                                 </StackLayout>  
  43.                             </ViewCell.View>  
  44.                         </ViewCell>  
  45.                     </DataTemplate>  
  46.                 </ListView.ItemTemplate>  
  47.             </ListView>  
  48.         </StackLayout>  
  49.     </ContentPage.Content>  
  50. </ContentPage>  
Step 7

In the code behind TasksPage.xaml.cs, update your code with this.
  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Threading.Tasks;  
  6. usingXamarin.Forms;  
  7. namespaceData_Entry  
  8. {  
  9.     publicpartialclassTasksPage: ContentPage  
  10.     {  
  11.         publicList < Member > Members  
  12.         {  
  13.             get;  
  14.             set;  
  15.         }  
  16.         publicTasksPage(List < Member > members)  
  17.         {  
  18.             Members = members;  
  19.             BindingContext = this;  
  20.             InitializeComponent();  
  21.         }  
  22.         publicasyncvoidOnSelected(objectobj, ItemTappedEventArgsargs)  
  23.         {  
  24.             var member = args.ItemasMember;  
  25.             awaitDisplayAlert("You selected", member.FirstName + " " + member.MiddleName + " " + member.LastName, "OK");  
  26.         }  
  27.     }  
  28. }  
Step 8

Run the application in your Android or Windows devices/emulators. When you save some records and then view them, then you will be navigated to the Tasks Page and see the following output.







Note


Since whole project has very high size, download the Portable Class Library (PCL) only from GitHub.

 

Up Next
    Ebook Download
    View all
    Learn
    View all