Skip to content

Latest commit

 

History

History

README.md

Overview

This tutorial demonstrates how to create a Xamarin mobile application for iOS and Android. The application uses the DevExpress ORM Library (XPO) to access and manage data and stores data in an SQLite database.

Prerequisites

  • Visual Studio 2019 with the Mobile development with .NET workload.
  • (Optional) A paired Mac to build the application on iOS.

Step 1: Create a Mobile App

  1. Open Visual Studio and create a new project.

  2. Search for the Mobile App (Xamarin Forms) template.

    Click Next to proceed.

  3. Set the project name to XamarinFormsDemo and click Create.

  4. Select the Master-Detail application template and click OK.

  5. In the Solution Explorer, remove the unnecessary files:

    • Models\HomeMenuItem.cs
    • Services\MockDataStore.cs
    • Views\MenuPage.xaml

For more information, see the following:

Step 2: Add the NuGet Packages

The application you build in this tutorial requires the following NuGet Packages:

  • DevExpress.Xpo
  • Microsoft.Data.Sqlite

From Visual Studio's Tools menu, select NuGet Package Manager > Package Manager Console.

Make sure Package source is set to All or nuget.org and run the following commands:

Install-Package DevExpress.Xpo
Install-Package Microsoft.Data.Sqlite

Step 3: Create ORM Data Model and Seed Initial Data

  1. Change the project's Item class (the "/Models/Item.cs" file) as follows:

    using DevExpress.Xpo;
    
    namespace XamarinFormsDemo.Models {
        [Persistent]
        public class Item {
            [Key]
            public string Id { get; set; }
            [Size(256)]
            public string Text { get; set; }
            [Size(SizeAttribute.Unlimited)]
            public string Description { get; set; }
        }
    }
  2. In the Solution Explorer, create the Core folder and add the XpoHelper.cs code file.

    Replace its content with the code copied from the file that contains the XpoHelper class.

    The XpoHelper class creates and saves initial data in the database.

For more information, see the following:

Step 4: Implement CRUD Operations and Connect App to SQLite

  1. In the Solution Explorer, select the Services folder and add the XpoDataStore.cs code file. Replace the code with the code from the corresponding file.

    The XpoDataStore class implements the IDataStore interface declared in the IDataStore.cs file.

    The following asynchronous XPO methods are used to implement CRUD operations in the XpoDataStore class: Session.GetObjectByKeyAsync, Session.Delete, UnitOfWork.CommitChangesAsync.

  2. Open the App.xaml.cs file and replace its content with the code copied from the corresponding file.

    The App() constructor's does the following:

    • specifies the path to the local application data where the SQLite data file must be stored
    • initializes XPO using the XpoInit method implemented in step 3
    • specifies the initially loaded application View.
  3. iOS-specific step. In the XamarinFormsDemo.iOS project, replace the Main.cs file content with the code copied from the corresponding file.

For more information, see the following:

Step 5: Modify View Models

  1. In the BaseViewModel ("/ViewModels/BaseViewModel.cs"), change the DataStore property declaration as follows:

    namespace XamarinFormsDemo.ViewModels {
        public class BaseViewModel : INotifyPropertyChanged {
            // change line
            public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>() ?? new XpoDataStore();
            // change line end
        // ...
        }
    }
  2. Open the ItemsViewModel.cs file and replace its content with the code copied from the corresponding file. This ViewModel passes the View's CRUD commands (see step 6) to the data store and returns the result back to the View.

Step 6: Modify Views

Open the Views folder in the Solution Explorer and replace the following files with the corresponding files from this folder:

  • ItemDetailPage.xaml
  • ItemDetailPage.xaml.cs
  • ItemsPage.xaml
  • ItemsPage.xaml.cs
  • NewItemPage.xaml
  • NewItemsPage.xaml.cs
  • MainPage.xaml
  • MainPage.xaml.cs

Step 7: Run and Test the App

Press F5 to build and run the application.

You can add new items in a separate form.

In Release mode, Xamarin iOS and Android apps that use XPO for data access can crash with TypeLoadException or MissingMethodException if you build with certain linker options. Refer to the following article to resolve this issue:

XPO - Recommended Xamarin Linker Options