Xamarin
Improve EF Core performance with EF Extensions
Xamarin
Xamarin.Forms is an open-source mobile UI framework from Microsoft for building iOS, Android, & Windows apps with .NET from a single shared codebase.
Xamarin.Forms is a feature of Xamarin, the popular mobile development framework that extends the .NET developer platform with tools and libraries for building mobile apps.
Use Xamarin.Forms built-in pages, layouts, and controls to build and design mobile apps from a single API that is highly extensible. Subclass any control to customize their behavior or define your own controls, layouts, pages, and cells to make your app pixel perfect.
Create a Xamarin App
To start, we will create a Xamarin project. The project type comes with all template files to create Xamarin application before adding anything. Let's open Visual Studio 2019, if you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

On the start window, choose Create a new project.

On the Create a new project window, enter or type xamarin.forms in the search box. Next, choose C# from the Language list. Select the Mobile App (Xamarin.Forms) template, and then choose Next.

In the Configure your new project window, type or enter EFCore5InXamarinApp in the Project name box and click on the Create button.

On the New Mobile App page, select the Flyout option, check the Andriod checkbox, and click on the Create button.

Visual Studio opens your new project and includes the default code files in your project as shown in the Solution Explorer.
Install Entity Framework Core
To use Entity Framework Core we need to install Microsoft.EntityFrameworkCore library. It is available as a nuget package and you can install it using Nuget Package Manager.
In the Package Manager Console window, enter the following command.
For SQLite, we need to install Microsoft.EntityFrameworkCore.Sqlite and will get all the packages required for EF Core.
Create a Data Model and Database Context
In Solution Explorer, right-click on the Models folder and choose Add > Class. Enter a class file name Author.cs and add the following code.
To add a context class, right-click on the Services folder in Solution Explorer, and choose Add > Class. Enter a class file name AuthorContext.cs and add the following code.
Add another class to the Services folder and name it AuthorDataStore.cs and add the following code.
It implements the IDataStore interface and contains all the required database operations. To get the implementation of IDataStore add the following code the BaseViewModel.
Add Views and ViewModels
Create New Author View and ViewModel
To create a new author view, right-click on the Views folder and select Add > New Item...

Select the Content Page template, enter NewAuthorPage.xaml in the Name field and click on the Add button. Replace the following code in NewAuthorPage.xaml file.
Now let's add a view model class for this page by adding a new class in the ViewModels folder, name it NewAuthorViewModel.cs and replace the following code.
Update the NewAuthorPage.xaml.cs to bind the view model with a view.
Create Author Detail View and ViewModel
To create an author detail view, right-click on the Views folder and select Add > New Item...

Select the Content Page template, enter AuthorDetailPage.xaml in the Name field and click on the Add button. Replace the following code in AuthorDetailPage.xaml file.
Now let's add a view model class for this page by adding a new class in the ViewModels folder, name it AuthorDetailViewModel.cs and replace the following code.
Update the AuthorDetailPage.xaml.cs to bind the view model with a view.
Create Authors View and ViewModel
To display all the authors from the database, right-click on the Views folder and select Add > New Item...

Select the Content Page template, enter AuthorsPage.xaml in the Name field and click on the Add button. Replace the following code in AuthorsPage.xaml file.
Now let's add a view model class for this page by adding a new class in the ViewModels folder, name it AuthorsViewModel.cs and replace the following code.
Update the AuthorsPage.xaml.cs to bind the view model with a view.
Let's update the App.xaml.cs to create and initialize the database with test data.
To register the route for the author pages, replace the following code in AppShell.xaml.cs
Now we also need to add the options to the menu in AppShell.xaml file to navigate to the Authors page.
Let's run your application and click on the Authors menu option.

To add a new author tap the ADD button which is on the top right corner, it will navigate to the New Author Page.

Enter Name and Address and click on the SAVE button and you will see a new author is added.

To view the detail, tap on an author and it will navigate to the detail page.

Last updated
