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.
- Visual Studio 2019 with the Mobile development with .NET workload.
- (Optional) A paired Mac to build the application on iOS.
-
Open Visual Studio and create a new project.
-
Search for the Mobile App (Xamarin Forms) template.
Click Next to proceed.
-
Set the project name to XamarinFormsDemo and click Create.
-
Select the Master-Detail application template and click OK.
-
In the Solution Explorer, remove the unnecessary files:
- Models\HomeMenuItem.cs
- Services\MockDataStore.cs
- Views\MenuPage.xaml
For more information, see the following:
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.XpoInstall-Package Microsoft.Data.Sqlite-
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; } } }
-
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
XpoHelperclass.The
XpoHelperclass creates and saves initial data in the database.
For more information, see the following:
-
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
XpoDataStoreclass implements theIDataStoreinterface declared in the IDataStore.cs file.The following asynchronous XPO methods are used to implement CRUD operations in the
XpoDataStoreclass: Session.GetObjectByKeyAsync, Session.Delete, UnitOfWork.CommitChangesAsync. -
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
XpoInitmethod implemented in step 3 - specifies the initially loaded application View.
-
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:
-
In the
BaseViewModel("/ViewModels/BaseViewModel.cs"), change theDataStoreproperty 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 // ... } }
-
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.
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
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
TypeLoadExceptionorMissingMethodExceptionif you build with certain linker options. Refer to the following article to resolve this issue:







