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.
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.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Text;namespaceEFCore5InXamarinApp.Models{publicclassAuthor { [DatabaseGenerated(DatabaseGeneratedOption.Identity)]publicstring Id { get; set; }publicstring Name { get; set; }publicstring Address { get; set; } }}
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.
It implements the IDataStore interface and contains all the required database operations. To get the implementation of IDataStore add the following code the BaseViewModel.
public IDataStore<Author> AuthDataStore =>DependencyService.Get<IDataStore<Author>>();
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.
usingEFCore5InXamarinApp.Models;usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Windows.Input;usingXamarin.Forms;namespaceEFCore5InXamarinApp.ViewModels{publicclassNewAuthorViewModel:BaseViewModel {privatestring name;privatestring address;publicNewAuthorViewModel() { SaveCommand =newCommand(OnSave, ValidateSave); CancelCommand =newCommand(OnCancel);this.PropertyChanged+= (_, __) =>SaveCommand.ChangeCanExecute(); }privateboolValidateSave() {return!String.IsNullOrWhiteSpace(name)&&!String.IsNullOrWhiteSpace(address); }publicstring Name {get=> name;set=>SetProperty(ref name, value); }publicstring Address {get=> address;set=>SetProperty(ref address, value); }publicCommand SaveCommand { get; }publicCommand CancelCommand { get; }privateasyncvoidOnCancel() { // This will pop the current page off the navigation stackawaitShell.Current.GoToAsync(".."); }privateasyncvoidOnSave() {Author newAuthor =newAuthor() { Id =Guid.NewGuid().ToString(), Name = Name, Address = Address };awaitAuthDataStore.AddItemAsync(newAuthor); // This will pop the current page off the navigation stackawaitShell.Current.GoToAsync(".."); } }}
Update the NewAuthorPage.xaml.cs to bind the view model with a view.
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.
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.