Xamarin.Forms - Deep Linking Application Using SQLite

Introduction
 
This article demonstrates how to create Deep Link applications using C# and XML in Xamarin.Forms.
 
Let's take an email client as an example. When the user clicks the notification of an email she received, it opens a deep link that takes her to the email in the app. Last but not least, deep links also allow Google to index your app and link to specific sections of your app in searches. The deep link appears as a search result in Google and can take the user to a particular section of your app. 
    
Let's start,
 
Step 1 
 
Create a Xamarin.Forms app by going to File >> New >> Visual C# >> Cross Platform >> Cross platform app (Xamarin.Native or Xamarin.Forms) and click OK. Project Name :DeepLinking
 
 
 
Now, select Blank App >> Xamarin.Forms >> .Portable Class Library and click OK.
 
Step 2
 
After the project creation, add the following Nuget Packages for your projects. Open Solution Explorer >> right click to solution explorer and select "Manage NuGet packages for the solution". 
 
Now, select the following NuGet packages and followed by select your project then click to Install it. 
  • SQLite-net-pcl
  • SQLitePCL.bundle_green
  • SQLitePCL.raw
  • SQLitePCL.Raw.bundle_green
  • SQLitePCLRaw.core.
 
Step 3
 
Next, open Solution Explorer >> Project Name(Portable) >> Right click to add >> New Folder . After that, open the New Folder  (Folder Name: Data)
 
  
 
Next, open Solution Explorer >> Project Name(Portable) >> Data Folder >> Right click to add >> Class . After that, open the Dialog Box Select Visual C# >> Class and click ok (Class Name: TodoItemDatabase).
 
 
 
The select TodoItemDatabase.cs of this page. Just replace that with the following code.
 
 
 
TodoItemDatabase.cs code 
  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using SQLite;  
  4. using Xamarin.Forms;  
  5.   
  6. namespace DeepLinking  
  7. {  
  8.     public class TodoItemDatabase  
  9.     {  
  10.         static readonly object locker = new object ();  
  11.         readonly SQLiteConnection database;  
  12.   
  13.         public TodoItemDatabase ()  
  14.         {  
  15.             database = DependencyService.Get<ISQLite> ().GetConnection ();  // DB of the application
  16.             database.CreateTable<TodoItem> ();                              // Create table for the DB
  17.         }  
  18.   
  19.         public IEnumerable<TodoItem> GetItems ()  
  20.         {  
  21.             lock (locker) {  
  22.                 return database.Table<TodoItem> ().ToList ();  
  23.             }  
  24.         }  
  25.   
  26.         public TodoItem Find (string id)  
  27.         {  
  28.             lock (locker) {  
  29.                 return database.Table<TodoItem> ().FirstOrDefault (i => i.ID == id);  
  30.             }  
  31.         }  
  32.   
  33.         public int Insert (TodoItem item)  
  34.         {  
  35.             lock (locker) {  
  36.                 return database.Insert (item);  
  37.             }  
  38.         }  
  39.   
  40.         public int Update (TodoItem item)  
  41.         {  
  42.             lock (locker) {  
  43.                 return database.Update (item);  
  44.             }  
  45.         }  
  46.   
  47.         public int Delete (string id)  
  48.         {  
  49.             lock (locker) {  
  50.                 return database.Delete<TodoItem> (id);  
  51.             }  
  52.         }  
  53.     }  
  54. }  
Similarly, add New Folder Models, and Views 
 
Step 4
 
Next, go to Solution Explorer >> Project Name (Portable) >> Models >> Right click to add >> Class. After that, open the Dialog Box Select Visual C# >> Class and click ok (Class Name: TodoItem.cs). The select TodoItem.cs of this page. Just replace that with the following code.
 
TodoItem.cs Code
  1. using SQLite;  
  2.   
  3. namespace DeepLinking  
  4. {  
  5.     public class TodoItem  
  6.     {  
  7.         [PrimaryKey]  
  8.         public string ID { getset; }  
  9.   
  10.         public string Name { getset; }  
  11.   
  12.         public string Notes { getset; }  
  13.   
  14.         public bool Done { getset; }  
  15.     }  
  16. }  
Step 5
 
Next, go to Solution Explorer >> Project Name (Portable) >> Views >> Right click to add >> two Class and twp Design Page. After that, open the Dialog Box Select Visual C# >> Class,Xamarin.Form and click ok.
(Class Name: TodoItemPage.xaml,TodoItemPageCS.cs,TodoListPage,xaml and TodoListPageCS.cs). To select the given page, just replace with  the following code.
 
TodoItemPage.xaml Code
 
Design for TodoItemPage.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DeepLinking.TodoItemPage" Title="Todo Item">  
  3.     <StackLayout VerticalOptions="StartAndExpand">  
  4.         <Label Text="Name" />  
  5.         <Entry Text="{Binding Path=Name}" Placeholder="task name" />  
  6.         <Label Text="Notes" />  
  7.         <Entry Text="{Binding Path=Notes}" />  
  8.         <Label Text="Done" />  
  9.         <Switch IsToggled="{Binding Path=Done}" />  
  10.         <Button Text="Save" Clicked="OnSaveActivated" />  
  11.         <Button Text="Delete" Clicked="OnDeleteActivated" />  
  12.         <Button Text="Cancel" Clicked="OnCancelActivated" />  
  13.     </StackLayout>  
  14. </ContentPage>  
TodoItemPage.xaml.cs Code 
 
This code used to the backend of the ItemPage. 
  1. using System;  
  2. using System.Net;  
  3. using Xamarin.Forms;  
  4.   
  5. namespace DeepLinking  
  6. {  
  7.     public partial class TodoItemPage : ContentPage  
  8.     {  
  9.         IAppLinkEntry appLink;  
  10.         bool isNewItem;  
  11.   
  12.         public TodoItemPage() : this(false)  
  13.         {  
  14.         }  
  15.   
  16.         public TodoItemPage(bool isNew = false)  
  17.         {  
  18.             InitializeComponent();  
  19.             isNewItem = isNew;  
  20.         }  
  21.   
  22.         protected override void OnAppearing()  
  23.         {  
  24.             appLink = GetAppLink(BindingContext as TodoItem);  
  25.             if (appLink != null)  
  26.             {  
  27.                 appLink.IsLinkActive = true;  
  28.             }  
  29.         }  
  30.   
  31.         protected override void OnDisappearing()  
  32.         {  
  33.             if (appLink != null)  
  34.             {  
  35.                 appLink.IsLinkActive = false;  
  36.             }  
  37.         }  
  38.   
  39.         async void OnSaveActivated(object sender, EventArgs e)  
  40.         {  
  41.             var todoItem = (TodoItem)BindingContext;  
  42.   
  43.             if (isNewItem)  
  44.             {  
  45.                 App.Database.Insert(todoItem);  
  46.             }  
  47.             else {  
  48.                 App.Database.Update(todoItem);  
  49.             }  
  50.   
  51.             appLink = GetAppLink(BindingContext as TodoItem);  
  52.             Application.Current.AppLinks.RegisterLink(appLink);  
  53.   
  54.             await Navigation.PopAsync();  
  55.         }  
  56.   
  57.         async void OnDeleteActivated(object sender, EventArgs e)  
  58.         {  
  59.             var todoItem = (TodoItem)BindingContext;  
  60.             App.Database.Delete(todoItem.ID);  
  61.             Application.Current.AppLinks.DeregisterLink(appLink);  
  62.   
  63.             await Navigation.PopAsync();  
  64.         }  
  65.   
  66.         async void OnCancelActivated(object sender, EventArgs e)  
  67.         {  
  68.             await Navigation.PopAsync();  
  69.         }  
  70.   
  71.         AppLinkEntry GetAppLink(TodoItem item)  
  72.         {  
  73.             var pageType = GetType().ToString();  
  74.             var pageLink = new AppLinkEntry  
  75.             {  
  76.                 Title = item.Name,  
  77.                 Description = item.Notes,  
  78.                 AppLinkUri = new Uri(string.Format("http://{0}/{1}?id={2}", App.AppName, pageType, WebUtility.UrlEncode(item.ID)), UriKind.RelativeOrAbsolute),  
  79.                 IsLinkActive = true,  
  80.                 Thumbnail = ImageSource.FromFile("monkey.png")  
  81.             };  
  82.   
  83.             pageLink.KeyValues.Add("contentType""TodoItemPage");  
  84.             pageLink.KeyValues.Add("appName", App.AppName);  
  85.             pageLink.KeyValues.Add("companyName""Xamarin");  
  86.   
  87.             return pageLink;  
  88.         }  
  89.     }  
  90. }  
TodoIteamPageCS.cs code 
 
This code used to the switched by the ItemPage.
  1. using System;  
  2. using System.Net;  
  3. using Xamarin.Forms;  
  4.   
  5. namespace DeepLinking  
  6. {  
  7.     public class TodoItemPageCS : ContentPage  
  8.     {  
  9.         IAppLinkEntry appLink;  
  10.         bool isNewItem;  
  11.   
  12.         public TodoItemPageCS() : this(false)  
  13.         {  
  14.         }  
  15.   
  16.         public TodoItemPageCS(bool isNew = false)  
  17.         {  
  18.             isNewItem = isNew;  
  19.   
  20.             var nameEntry = new Entry { Placeholder = "task name" };  
  21.             nameEntry.SetBinding(Entry.TextProperty, "Name");  
  22.   
  23.             var notesEntry = new Entry();  
  24.             notesEntry.SetBinding(Entry.TextProperty, "Notes");  
  25.   
  26.             var doneSwitch = new Switch();  
  27.             doneSwitch.SetBinding(Switch.IsToggledProperty, "Done");  
  28.   
  29.             var saveButton = new Button { Text = "Save" };  
  30.             saveButton.Clicked += OnSaveActivated;  
  31.             var deleteButton = new Button { Text = "Delete" };  
  32.             deleteButton.Clicked += OnDeleteActivated;  
  33.             var cancelButton = new Button { Text = "Cancel" };  
  34.             cancelButton.Clicked += OnCancelActivated;  
  35.   
  36.             Title = "Todo Item";  
  37.             Content = new StackLayout  
  38.             {  
  39.                 VerticalOptions = LayoutOptions.StartAndExpand,  
  40.                 Children = {  
  41.                     new Label { Text = "Name" },  
  42.                     nameEntry,  
  43.                     new Label { Text = "Notes" },  
  44.                     notesEntry,  
  45.                     new Label { Text = "Done" },  
  46.                     doneSwitch,  
  47.                     saveButton,  
  48.                     deleteButton,  
  49.                     cancelButton  
  50.                 }  
  51.             };  
  52.         }  
  53.   
  54.         protected override void OnAppearing()  
  55.         {  
  56.             appLink = GetAppLink(BindingContext as TodoItem);  
  57.             if (appLink != null)  
  58.             {  
  59.                 appLink.IsLinkActive = true;  
  60.             }  
  61.         }  
  62.   
  63.         protected override void OnDisappearing()  
  64.         {  
  65.             if (appLink != null)  
  66.             {  
  67.                 appLink.IsLinkActive = false;  
  68.             }  
  69.         }  
  70.   
  71.         async void OnSaveActivated(object sender, EventArgs e)  
  72.         {  
  73.             var todoItem = (TodoItem)BindingContext;  
  74.   
  75.             if (isNewItem)  
  76.             {  
  77.                 App.Database.Insert(todoItem);  
  78.             }  
  79.             else {  
  80.                 App.Database.Update(todoItem);  
  81.             }  
  82.   
  83.             appLink = GetAppLink(BindingContext as TodoItem);  
  84.             Application.Current.AppLinks.RegisterLink(appLink);  
  85.   
  86.             await Navigation.PopAsync();  
  87.         }  
  88.   
  89.         async void OnDeleteActivated(object sender, EventArgs e)  
  90.         {  
  91.             var todoItem = (TodoItem)BindingContext;  
  92.             App.Database.Delete(todoItem.ID);  
  93.             Application.Current.AppLinks.DeregisterLink(appLink);  
  94.   
  95.             await Navigation.PopAsync();  
  96.         }  
  97.   
  98.         async void OnCancelActivated(object sender, EventArgs e)  
  99.         {  
  100.             await Navigation.PopAsync();  
  101.         }  
  102.   
  103.         AppLinkEntry GetAppLink(TodoItem item)  
  104.         {  
  105.             var pageType = GetType().ToString();  
  106.             var pageLink = new AppLinkEntry  
  107.             {  
  108.                 Title = item.Name,  
  109.                 Description = item.Notes,  
  110.                 AppLinkUri = new Uri(string.Format("http://{0}/{1}?id={2}", App.AppName, pageType, WebUtility.UrlEncode(item.ID)), UriKind.RelativeOrAbsolute),  
  111.                 IsLinkActive = true,  
  112.                 Thumbnail = ImageSource.FromFile("monkey.png")  
  113.             };  
  114.   
  115.             pageLink.KeyValues.Add("contentType""TodoItemPage");  
  116.             pageLink.KeyValues.Add("appName", App.AppName);  
  117.             pageLink.KeyValues.Add("companyName""Xamarin");  
  118.   
  119.             return pageLink;  
  120.         }  
  121.     }  
  122. }  
TodoListPage.xaml Code
 
Design for TodoListPage. 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DeepLinking.TodoListPage" Title="Todo">  
  3.     <ContentPage.ToolbarItems>  
  4.         <ToolbarItem Text="+" Clicked="OnAddItemClicked">  
  5.             <ToolbarItem.Icon>  
  6.                 <OnPlatform x:TypeArguments="FileImageSource">  
  7.         <On Platform="Android" Value="plus.png" />  
  8.     </OnPlatform>  
  9.             </ToolbarItem.Icon>  
  10.         </ToolbarItem>  
  11.     </ContentPage.ToolbarItems>  
  12.     <ListView x:Name="listView" ItemSelected="OnItemSelected">  
  13.         <ListView.ItemTemplate>  
  14.             <DataTemplate>  
  15.                 <ViewCell>  
  16.                     <StackLayout Padding="20,0,0,0" HorizontalOptions="StartAndExpand" Orientation="Horizontal">  
  17.                         <Label Text="{Binding Name}" VerticalTextAlignment="Center" />  
  18.                         <Image Source="check.png" IsVisible="{Binding Done}" />  
  19.                     </StackLayout>  
  20.                 </ViewCell>  
  21.             </DataTemplate>  
  22.         </ListView.ItemTemplate>  
  23.     </ListView>  
  24. </ContentPage>  
TodoListPage.xaml.cs Code
 
This code used to the backend of the ListPage.
  1. using System;  
  2. using Xamarin.Forms;  
  3.   
  4. namespace DeepLinking  
  5. {  
  6.     public partial class TodoListPage : ContentPage  
  7.     {  
  8.         public TodoListPage()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.   
  13.         protected override void OnAppearing()  
  14.         {  
  15.             base.OnAppearing();  
  16.             listView.ItemsSource = App.Database.GetItems();  
  17.         }  
  18.   
  19.         protected override void OnDisappearing()  
  20.         {  
  21.             base.OnDisappearing();  
  22.             listView.ItemsSource = null;  
  23.         }  
  24.   
  25.         async void OnAddItemClicked(object sender, EventArgs e)  
  26.         {  
  27.             var todoItem = new TodoItem()  
  28.             {  
  29.                 ID = Guid.NewGuid().ToString()  
  30.             };  
  31.             var todoPage = new TodoItemPage(true);  
  32.             todoPage.BindingContext = todoItem;  
  33.             await Navigation.PushAsync(todoPage);  
  34.         }  
  35.   
  36.         async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)  
  37.         {  
  38.             var todoItem = e.SelectedItem as TodoItem;  
  39.             var todoPage = new TodoItemPage  
  40.             {  
  41.                 BindingContext = todoItem  
  42.             };  
  43.             await Navigation.PushAsync(todoPage);  
  44.         }  
  45.     }  
  46. }  
TodoListPageCS.cs code
 
This code used to the switched by the ListPage. 
  1. using System;  
  2. using Xamarin.Forms;  
  3.   
  4. namespace DeepLinking  
  5. {  
  6.     public partial class TodoListPageCS : ContentPage  
  7.     {  
  8.         ListView listView;  
  9.   
  10.         public TodoListPageCS()  
  11.         {  
  12.             Title = "Todo";  
  13.   
  14.             var toolbarItem = new ToolbarItem { Text = "+" };  
  15.             toolbarItem.Clicked += OnAddItemClicked;  
  16.             toolbarItem.Icon = Device.RuntimePlatform == Device.Android ? "plus.png" : null;  
  17.             ToolbarItems.Add(toolbarItem);  
  18.   
  19.             var dataTemplate = new DataTemplate(() =>  
  20.             {  
  21.                 var label = new Label { VerticalTextAlignment = TextAlignment.Center };  
  22.                 label.SetBinding(Label.TextProperty, "Name");  
  23.   
  24.                 var image = new Image { Source = ImageSource.FromFile("check.png") };  
  25.                 image.SetBinding(VisualElement.IsVisibleProperty, "Done");  
  26.   
  27.                 var stackLayout = new StackLayout  
  28.                 {  
  29.                     Padding = new Thickness(20, 0, 0, 0),  
  30.                     HorizontalOptions = LayoutOptions.StartAndExpand,  
  31.                     Orientation = StackOrientation.Horizontal,  
  32.                     Children = {  
  33.                         label,  
  34.                         image  
  35.                     }  
  36.                 };  
  37.   
  38.                 return new ViewCell { View = stackLayout };  
  39.             });  
  40.   
  41.             listView = new ListView { ItemTemplate = dataTemplate };  
  42.             listView.ItemSelected += OnItemSelected;  
  43.   
  44.             Content = listView;  
  45.         }  
  46.   
  47.         protected override void OnAppearing()  
  48.         {  
  49.             base.OnAppearing();  
  50.             listView.ItemsSource = App.Database.GetItems();  
  51.         }  
  52.   
  53.         protected override void OnDisappearing()  
  54.         {  
  55.             base.OnDisappearing();  
  56.             listView.ItemsSource = null;  
  57.         }  
  58.   
  59.         async void OnAddItemClicked(object sender, EventArgs e)  
  60.         {  
  61.             var todoItem = new TodoItem()  
  62.             {  
  63.                 ID = Guid.NewGuid().ToString()  
  64.             };  
  65.             var todoPage = new TodoItemPageCS(true);  
  66.             todoPage.BindingContext = todoItem;  
  67.             await Navigation.PushAsync(todoPage);  
  68.         }  
  69.   
  70.         async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)  
  71.         {  
  72.             var todoItem = e.SelectedItem as TodoItem;  
  73.             var todoPage = new TodoItemPageCS  
  74.             {  
  75.                 BindingContext = todoItem  
  76.             };  
  77.             await Navigation.PushAsync(todoPage);  
  78.         }  
  79.     }  
  80. }  
Finally, we will see the given classes.
 
 
 
Step 6 
 
Open Solution Explorer >> right click to solution explorer and select Add >> Class.
 
 
 
After that, open the Dialog Box Select Visual C# >> Class and click ok (Class Name: ISQLite). 
 
 
 
The select ISQLite.cs of this page. Just copy the following code.
 
 
 
ISQLite Code
 
Connecting to the SQLite DB
  1. SQLiteConnection GetConnection ();   
Step 7
 
Next,go to Solution Explorer >> Project Name(Portable) >> App.cs, The select App.cs of this page. Just copy the following code.
 
App.cs Code 
  1. public App()  
  2.         {  
  3.             Database = new TodoItemDatabase();  
  4.             MainPage = new NavigationPage(new TodoListPage());  
  5.         }  
Step 8
 
After completing your work with the design view, go to " Build " menu and click open "Configure Manager ". In the popup window, configure your startup projects. Click F5 or Run your project. Given below is the result.
 
 
 
Finally, we have successfully created a DeepLinking application. Later we will discuss more Xamarin.Forms applications.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now