Introduction:
In this tutorial, we will learn how to perform MVVM approach in Xamarin.Forms using Fresh MVVM. MVVM approach is the best approach for Xamarin.Forms and WPF Applications. There are a lot of MVVM plugins or libraries like FreshMVVM, MVVMCross, Prism, etc. available to simplify MVVM implementations.
FreshMVVM:
FreshMVVM is designed to perform MVVM easy and simple with Xamarin.Forms Application. It is created Michael Ridland. It has certain rules to perform MVVM Databinding. You can find the plugin from GitHub and Nuget.
Kindly refer my previous article on Fresh MVVM to know the basics & rules of Fresh MVVM.
Kindly refer my previous article on Fresh MVVM to know the basics & rules of Fresh MVVM.
Coding Part:
Steps:
I have split this part into 3 steps as in the following.
- Creating new Xamarin.Forms Projects.
- Setting up the plugin for Xamarin.Forms Application.
- Implementing Fresh MVVM.
Step 1: Creating new Xamarin.Forms Projects
Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.
Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.
Step 2: Setting up the plugin for Xamarin.Forms Application
We will start coding for Fresh MVVM. Create New Xamarin Forms Project. Open Nuget Package Manager against the solution and do search for Fresh MVVM Plugin or Paste the following Nuget Installation.
Install-Package FreshMvvm -Version 2.2.3
Step 3: Implementing Fresh MVVM.
- Create your XAML page (view) with name ended up with “Page”.
- Create PageModel by create Class name ended with “PageModel” and inherited with “FreshBasePageModel” as shown below screenshot.
- In the same way, I have created two pages “Detail1Page” and “Detail2Page” with two respective page models “Detail1PageModel” and “Detail2PageModel”.
Set MainPage:
To create Fresh MVVM Master Detail Page as MainPage, we should use Fresh Master Detail Navigation Container with the following code. Open App.xaml.cs or App.cs and set MainPage.var masterNavigation = new FreshMasterDetailNavigationContainer();
masterNavigation.Init("Menu");
masterNavigation.AddPage("First Page", null);
masterNavigation.AddPage("Second Page", null);
MainPage = masterNavigation;
- Here, we will not discuss about navigation between pages. If you want to know, you can see my previous article on Fresh MVVM.
Full Code for App.xaml.cs:
You can find the code for App.xaml.cs belowpublic partial class App : Application
{
public App()
{
try
{
InitializeComponent();
var masterNavigation = new FreshMasterDetailNavigationContainer();
masterNavigation.Init("Menu");
masterNavigation.AddPage("First Page", null);
masterNavigation.AddPage("Second Page", null);
MainPage = masterNavigation;
}
catch (Exception ex)
{
}
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
Serves24 is Best refrigerator repair In Hyderabad We Provided Door Step Repair And Service & Provide best in class. Get 6 months repair guarantee.
ReplyDeleteBook Now just @249/-. at most affordable prices
LG refrigerator repair in Hyderabad
refrigerator repair in Hyderabad
fridges repair Serivces
Samsung refrigerator repair in Hyderabad
IFB washing machine repair in Hyderabad
fridges repairs hyderabad