![]() |
This step is very similar to the previous one. The only difference is that the code will change a little due to the fact that the location of the database file will be different. You will still need to add the appropriate packages to the Android project (SQLite.Net PCL and SQLite.NET PCL - XamarinAndroid) as you did before. Once you have completed that, you can add the appropriate code in a new class named SQLite_Android.
- using System;
- using System.IO;
- using Xamarin.Forms;
- using IntroToSQLite.Android;
- [assembly: Dependency(typeof(SQLite_Android))]
- namespace IntroToSQLite.Android
- {
- public class SQLite_Android: ISQLite
- {
- public SQLite_Android ()
- {
- }
- #region ISQLite implementation
- public SQLite.Net.SQLiteConnection GetConnection ()
- {
- var fileName = "RandomThought.db3";
- var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
- var path = Path.Combine (documentsPath, fileName);
- var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid ();
- var connection = new SQLite.Net.SQLiteConnection (platform, path);
- return connection;
- }
- #endregion
- }
- }
Step 6: Add the Windows Phone Implementation
Since I am running this app from a Mac, I won't be creating the Windows Phone implementation, but if you would like to do this, you can.
The first step is to add support to your Windows Phone project for SQLite. As mentioned earlier, SQLite comes by default on iOS and Android. This is not true for Windows Phone, but it is supported. To get it installed, you can follow the instructions found on the Xamarin website.
After installing SQLite, the process of adding the functionality for Windows Phone will be almost exactly the same, except that the packages to install are SQLite.Net PCL and SQLite.Net PCL - WindowsPhone 8 Platform. With these packages installed, you can create the Windows Phone implementation of the ISQLite interface.
- using System;
- using System.IO;
- using Xamarin.Forms;
- using IntroToSQLite.WinPhone;
- [assembly: Dependency(typeof(SQLite_WinPhone)]
- namespace IntroToSQLite.WinPhone
- {
- public class SQLite_WinPhone: ISQLite
- {
- public SQLite_WinPhone ()
- {
- }
- #region ISQLite implementation
- public SQLite.Net.SQLiteConnection GetConnection ()
- {
- var fileName = "RandomThought.db3";
- var path = Path.Combine (ApplicationData.Current.LocalFolder.Path, fileName);
- var platform = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8 ();
- var connection = new SQLite.Net.SQLiteConnection (platform, path);
- return connection;
- }
- #endregion
- }
- }
Step 7: Adding the User Interface
Since this tutorial is well into the topic of Xamarin.Forms, I'm going to assume that you at least have a basic working knowledge of Xamarin.Forms. With this assumption in mind, I'm not going to go into a lot of detail on the process of creating the user interface. If you need more background information on Xamarin.Forms, check out my other Xamarin.Forms tutorials on Tuts+.
The user interface is going to consist of two separate pages. The first page will contain a list of all the thoughts we have entered in a list while the second page will let the user enter a new thought. Let's build these pages.
Create the ListView
We will first focus on creating the first page that will contain a list of RandomThought objects. Start by creating a new file in the PCL (or Shared) project and name it RandomThoughtsPage. Replace the default implementation with the following:
- using System;
- using Xamarin.Forms;
- namespace IntroToSQLite
- {
- public class RandomThoughtsPage: ContentPage {
- private RandomThoughtDatabase _database;
- private ListView _thoughtList;
- public RandomThoughtsPage (RandomThoughtDatabase database)
- {
- _database = database;
- Title = "Random Thoughts";
- var thoughts = _database.GetThoughts ();
- _thoughtList = new ListView ();
- _thoughtList.ItemsSource = thoughts;
- _thoughtList.ItemTemplate = new DataTemplate (typeof(TextCell));
- _thoughtList.ItemTemplate.SetBinding (TextCell.TextProperty, "Thought");
- _thoughtList.ItemTemplate.SetBinding (TextCell.DetailProperty, "CreatedOn");
- var toolbarItem = new ToolbarItem {
- Name = "Add",
- Command = new Command(() => Navigation.PushAsync(new ThoughtEntryPage(this, database)))
- };
- ToolbarItems.Add (toolbarItem);
- Content = _thoughtList;
- }
- public void Refresh() {
- _thoughtList.ItemsSource = _database.GetThoughts ();
- }
- }
- }
To get our new RandomThoughtsPage up on the screen, we need to make a little modification to the App.cs file. Within this file, modify the GetMainPage method to look like the following:
- public static Page GetMainPage ()
- {
- var database = new RandomThoughtDatabase ();
- return new NavigationPage (new RandomThoughtsPage (database));
- }
![]() |
![]() |
We now have a list page for all of our RandomThought objects, but we don't have a way to enter new ones. For that, we will create another page similar to the previous page. Create a new file in your PCL (or Shared) project and call it ThoughtEntryPage. Replace the default implementation with the following:
- using System;
- using Xamarin.Forms;
- namespace IntroToSQLite
- {
- public class ThoughtEntryPage: ContentPage {
- private RandomThoughtsPage _parent;
- private RandomThoughtDatabase _database;
- public ThoughtEntryPage ( RandomThoughtsPage parent, RandomThoughtDatabase database)
- {
- _parent = parent;
- _database = database;
- Title = "Enter a Thought";
- var entry = new Entry ();
- var button = new Button {
- Text = "Add"
- };
- button.Clicked += async (object sender, EventArgs e) => {
- var thought = entry.Text;
- _database.AddThought(thought);
- await Navigation.PopAsync();
- _parent.Refresh();
- };
- Content = new StackLayout {
- Spacing = 20,
- Padding = new Thickness(20),
- Children = { entry, button },
- };
- }
- }
- }
Once the user taps the Button, we use the database to add the new thought, dismiss the page, go back to the list page, and call the Refresh method to update the ListView. Once this is all wired up, we can run it to actually enter some values.
Entering Thoughts
Here is what it looks like on iOS and Android to enter some of your thoughts:
![]() |
![]() |
After you have entered a few thoughts, your list will look something like this:
![]() |
![]() |
There you have it. You now have the ability to add database functionality to your Xamarin.Forms app to store and retrieve any sort of data with ease.
To continue your learning journey with Xamarin.Forms and SQLite, I give you the following challenge. See if you can enhance this application to enable deleting thoughts and update the list page in a similar fashion as the entry page. Good luck and happy coding.
Written by Derek Jensen
If you found this post interesting, follow and support us.
Suggest for you:
The Complete Android & Java Course - Build 21 Android Apps
Android Application Programming - Build 20+ Android Apps
iOS 10 Message Extension App Reskinning without Coding
iOS 10 Projects: Build Amazing Apps with Apple's Newest iOS
iOS 10 & Swift 3: From Beginner to Paid Professional







No comments:
Post a Comment