-
Notifications
You must be signed in to change notification settings - Fork 2
Create New Page
Bader Albarrak edited this page Jul 31, 2020
·
5 revisions
In order to create a new page and follow MVVM Design Pattern.
You have to do 4 things
- Create
ContentPage
and make sure it inherit from 'BaseContentPage` - Create
ViewModel
for that page and make sure it inherited fromBaseViewModel
- Add
DI
for theViewModel
- Map
ContentPage
withViewModel
you have created
Lets assume we want to create a ContentPage
with a name LoginPage
- Create a new
ContentPage
under theViews
Folder and name itLoginPage
- Modify the code behind of the page and make it inherit from
BaseContentPage
page like below code
public partial class LoginPage : BaseContentPage
{
public LoginPage()
{
InitializeComponent();
}
}
- Modify the
xaml
of the page by addinglocal
namespace as the followings
<local:BaseContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:local="clr-namespace:XamarinAppTemplate"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Login Page"
x:Class="XamarinAppTemplate.Views.LoginPage">
</local:BaseContentPage>
- Create a new class called
LoginViewModel
under the folderViewModels
and make sure it inherited fromBaseViewModel
as followings
public class LoginViewModel : BaseViewModel
{
public LoginViewModel()
{
}
}
- Add DI for the view model by modify the method
ConfigureServices
inStartup.cs
under folderConfigurations
like below
serviceCollections.AddTransient<LoginViewModel>();
- Map the page to the view model by modify the method
RegisterRoutes()
inNavigationService.cs
under the folderServices
to be like below
XamarinAppTemplateRoute.Register(typeof(LoginViewModel), typeof(LoginPage));
Note:
the name maybe different based on your project name
Now all sets and the page is ready to be navigated to
From any ViewModel
you can navigate to the page by calling await _navService.GoToAsync<LoginViewModel>();