Mobile Application Development Tutorial - MAUI
About This Tutorial
You must have an ABP Team or a higher license to be able to create a mobile application.
This tutorial assumes that you have completed the Web Application Development tutorial and built an ABP based application named Acme.BookStore
with MAUI as the mobile option. Therefore, if you haven't completed the Web Application Development tutorial, you either need to complete it or download the source code from down below and follow this tutorial.
In this tutorial, we will only focus on the UI side of the Acme.BookStore
application and we will implement the CRUD operations for a MAUI mobile application. This tutorial follows the MVVM (Model-View-ViewModel) Pattern , which separates the UI from the business logic of an application.
Download the Source Code
You can use the following link to download the source code of the application described in this article:
If you encounter the "filename too long" or "unzip" error on Windows, please see this guide.
Create the Authors Page - List & Delete Authors
Create a content page, AuthorsPage.xaml
under the Pages
folder of the Acme.BookStore.Maui
project and change the content as given below:
AuthorsPage.xaml
This is a simple page that lists Authors, allows opening a create modal to create a new author, editing an existing one and deleting one.
AuthorsPage.xaml.cs
Let's create the AuthorsPage.xaml.cs
code-behind class and copy paste the content below:
Here, we register our pages with a Transient
lifetime, so we can use it later on for navigation purposes and indicating the binding source (BindingContext
) as the AuthorPageViewModel
to get full benefit from the MVVM pattern. We haven't created the AuthorPageViewModel
class yet, so let's create it.
AuthorPageViewModel.cs
Create a view model class, AuthorPageViewModel
under the ViewModels
folder of the project and change the content as follows:
The AuthorPageViewModel
class is where all the logic behind the Authors
page lays. Here, we do the following steps:
- We have fetched all authors from the database and set those records into the
Items
property, which is a type ofObservableCollection<AuthorDto>
, so whenever the authors list changes then the CollectionView will be refreshed. - We have defined the
OpenCreateModal
andOpenEditModal
methods to navigate to the create modal and edit modal pages (we will create them in the following sections). - We have defined the
ShowActions
method to allow editing or deleting a specific author. - We have created the
Delete
method, which deletes a specific author and re-renders the grid. - And finally, we have implemented the
IRecipient<AuthorCreateMessage>
andIRecipient<AuthorEditMessage>
interfaces to refresh the grid after creating a new author or editing an existing one. (We will create theAuthorCreateMessage
andAuthorEditMessage
classes in the following sections)
Registering Author Page Routes
Open the AppShell.xaml.cs
file under the Acme.BookStore.Maui
project and register the create modal and edit modal pages' routes:
Since we need to navigate to the create modal and edit modal pages whenever the action buttons are clicked, we need to register those pages with their routes. We can do this in the AppShell.xaml.cs
file, which is responsible for providing the navigation of the application.
Creating a New Author
Create a new content page, AuthorCreatePage.xaml
under the Pages
folder of the Acme.BookStore.Maui
project and change the content as given below:
AuthorCreatePage.xaml
In this page, we have defined the form elements that are needed to create an author such as Name
, ShortBio
and BirthDate
. Whenever a user clicks the Save button, the CreateCommand will be triggered and will create a new author, if the operation goes successfully.
Let's define the AuthorCreateViewModel
as the BindingContext of this page and then define the logic of the CreateCommand.
AuthorCreatePage.xaml.cs
Create the AuthorCreatePage.xaml.cs
code-behind class and copy paste the content below:
AuthorCreateViewModel.cs
Create a view model class, AuthorCreateViewModel
under the ViewModels
folder of the project and change the content as follows:
Here, we do the following steps:
- This class simply injects and uses the
IAuthorAppService
to create a new author. - We have created two methods for the actions in the AuthorCreatePage, which are the
Cancel
andCreate
methods. - The
Cancel
method simply returns to the previous page, AuthorPage. - The
Create
method creates a new author whenever the Save button is clicked on the AuthorCreatePage.
AuthorCreateMessage.cs
Create a class, AuthorCreateMessage
under the Messages
folder of the project and change the content as follows:
This class is used to represent a message that we're gonna use to trigger a return result after author creation. Then, we subscribe to this message and update the grid on the AuthorsPage.
Updating an Author
Create a new content page, AuthorEditPage.xaml
under the Pages
folder of the Acme.BookStore.Maui
project and change the content as given below:
AuthorEditPage.xaml
In this page, we have defined the form elements that are needed to edit an author such as Name
, ShortBio
and BirthDate
. Whenever a user clicks the Save button, the UpdateCommand will be triggered and will update an existing author, if the operation goes successfully.
Let's define the AuthorEditViewModel
as the BindingContext of this page and then define the logic of the UpdateCommand.
AuthorEditPage.xaml.cs
Create the AuthorEditPage.xaml.cs
code-behind class and copy paste the content below:
AuthorEditViewModel.cs
Create a view model class, AuthorEditViewModel
under the ViewModels
folder of the project and change the content as follows:
Here, we do the following steps:
- This class simply injects and uses the
IAuthorAppService
to update an existing author. - We have created three methods for the actions in the AuthorEditPage, which are the
GetAuthor
,Cancel
andUpdate
methods. - The
GetAuthor
method is used to get the author from theId
query parameter and set it to theAuthor
property. - The
Cancel
method simply returns to the previous page, AuthorPage. - The
Update
method updates an existing author whenever the Save button is clicked on the AuthorEditPage.
AuthorEditMessage.cs
Create a class, AuthorEditMessage
under the Messages
folder of the project and change the content as follows:
This class is used to represent a message that we're gonna use to trigger a return result after an author is updated. Then, we subscribe to this message and update the grid on the AuthorsPage.
Add Author Menu Item to the Main Menu
Open the AppShell.xaml
file and add the following code under the Settings menu item:
AppShell.xaml
This code block adds a new Authors menu item under the Settings menu item. We need to show this menu item only when the required permission is granted. So, let's update the ShellViewModel.cs
class and check if the permission is granted or not.
ShellViewModel.cs
Create the Books Page - List & Delete Books
Create a new content page, BooksPage.xaml
under the Pages
folder of the Acme.BookStore.Maui
project and change the content as given below:
BooksPage.xaml
This is a simple page that lists books, allows opening a create modal to create a new book, editing an existing one and deleting one.
BooksPage.xaml.cs
Create the BooksPage.xaml.cs
code-behind class and copy paste content below:
BookPageViewModel.cs
Create a view model class, BookPageViewModel
under the ViewModels
folder of the project and change the content as follows:
The BookPageViewModel
class is where all the logic behind the Books
page lays. Here, we do the following steps:
- We have fetched all the books from the database and set those records into the
Items
property, which is a type ofObservableCollection<BookDto>
, so whenever the book list changes then the CollectionView will be refreshed. - We have defined the
OpenCreateModal
andOpenEditModal
methods to navigate to the create modal and edit modal pages (we will create them in the following sections). - We have defined the
ShowActions
method to allow editing or deleting a certain book. - We have created the
Delete
method, which deletes a specific book and re-renders the grid. - And finally, we have implemented the
IRecipient<BookCreateMessage>
andIRecipient<BookEditMessage>
interfaces to refresh the grid after creating a new book or editing an existing one. (We will create theBookCreateMessage
andBookEditMessage
classes in the following sections)
Registering Book Page Routes
Open the AppShell.xaml.cs
file under the Acme.BookStore.Maui
project and register the create modal and edit modal page routes:
Since, we need to navigate to the create modal and edit modal pages whenever the action buttons are clicked, we need to register those pages with their routes. We can do this in the AppShell.xaml.cs
file, which is responsible for providing the navigation of the application.
Creating a New Book
Create a new content page, BookCreatePage.xaml
under the Pages
folder of the Acme.BookStore.Maui
project and change the content as given below:
BookCreatePage.xaml
In this page, we have defined the form elements that are needed to create a book such as Name
, Type
, AuthorId
and PublishDate
. Whenever a user clicks the Save button, the CreateCommand will be triggered and will create a new book, if the operation goes successfully.
Let's define the BookCreateViewModel
as the BindingContext of this page and then define the logic of the CreateCommand.
BookCreatePage.xaml.cs
BookCreateViewModel.cs
Create a view model class, BookCreateViewModel
under the ViewModels
folder of the project and change the content as follows:
Here, we do the following steps:
- This class simply injects and uses the
IBookAppService
to create a new book. - We have created two methods for the actions in the BookCreatePage, which are the
Cancel
andCreate
methods. - The
Cancel
method simply returns to the previous page, BooksPage. - The
Create
method creates a new book whenever the Save button is clicked on the BookCreatePage.
BookCreateMessage.cs
Create a class, BookCreateMessage
under the Messages
folder of the project and change the content as follows:
This class is used to represent a message that we're gonna use to trigger a return result after book creation. Then, we subscribe to this message and update the grid on the BooksPage.
Updating a Book
Create a new content page, BookEditPage.xaml
under the Pages
folder of the Acme.BookStore.Maui
project and change the content as given below:
BookEditPage.xaml
In this page, we have defined the form elements that are needed to edit a book such as Name
, Type
, AuthorId
and PublishDate
. Whenever a user clicks the Save button, the UpdateCommand will be triggered and will update an existing book, if the operation goes successfully.
Let's define the BookEditViewModel
as the BindingContext of this page and then define the logic of the UpdateCommand.
BookEditPage.xaml.cs
BookEditViewModel.cs
Create a view model class, BookEditViewModel
under the ViewModels
folder of the project and change the content as follows:
Here, we do the following steps:
- This class simply injects and uses the
IBookAppService
to updating an existing book. - We have created four methods for the actions in the BookEditPage, which are the
GetBook
,GetAuthors
,Cancel
andUpdate
methods. - The
GetBook
method is used to get the book from theId
query parameter and set it to theBook
property. - The
GetAuthors
method is used to get the author lookup to list the authors in a picker. - The
Cancel
method simply returns to the previous page, BooksPage. - The
Update
method updates an existing book whenever the Save button is clicked on the BookEditPage.
BookEditMessage.cs
Create a class, BookEditMessage
under the Messages
folder of the project and change the content as follows:
This class is used to represent a message that we're gonna use to trigger a return result after a book is updated. Then, we subscribe to this message and update the grid on the BooksPage.
Add Book Menu Item to the Main Menu
Open the AppShell.xaml
file and add the following code before the Authors menu item:
AppShell.xaml
This code block adds a new Books menu item before the Authors menu item. We need to show this menu item only when the required permission is granted. So, let's update the ShellViewModel.cs
class and check if the permission is granted or not.
ShellViewModel.cs
Run the Application
That's all! You can run the application and login. Then you can navigate through pages, list, create, update and/or delete authors and books.