Showing posts with label Rg.Plugin.Popup. Show all posts

Rg Popup in Xamarin.Forms using Fresh MVVM

Introduction:

In this tutorial, we will learn how to use Rg.Plugin.Popup in Xamarin.Forms using Fresh MMVM. In my previous articles, we have learned how to use Fresh MVVM with Navigation Page, Master Detail Page and Tabbed Page. If you are new to Fresh MVVM, kindly read my previous articles 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.

  1. Creating new Xamarin.Forms Projects.
  2. Setting up the plugin for Xamarin.Forms Application.
  3. 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.

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

Click Install to install this Plugin against your PCL Project or .NET standard Project. Then download the Rg.Plugin.Popup by using the following Nuget package.

Install-Package Rg.Plugins.Popup -Version 1.1.5.188

Click Install to install this Plugin against your PCL Project or .NET standard Project and all dependent platform projects.

Step 3: Implementing Fresh MVVM Page & Page Models

  1. Create your XAML page (view) with name ended up with “Page”.
  2. Create PageModel by create Class name ended with “PageModel” and inherited with “FreshBasePageModel” as shown below screenshot.
  3. I have created a Page & PageModel named as “MainPage” & “MainPageModel” and set this page as Main Page/Root Page of the application as shown in the following.

    Set MainPage:

    We need to set the MainPageModel as MainPage using FreshNavigationConatiner. Open App.xaml.cs or App.cs and set MainPage.

    // To set MainPage for the Application
    var page = FreshPageModelResolver.ResolvePageModel();
    var basicNavContainer = new FreshNavigationContainer(page);
    MainPage = basicNavContainer;

  4. 4. Then create a Popup Page using Rg.Plugins.Popup by adding “”. The following code snippet shows how to create popup using Rg.Plugin. I have created a Xamarin.Forms Page and named as “MyPopupPage.xaml”.
    <?xml version="1.0" encoding="utf-8" ?>
    <popup:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
                     CloseWhenBackgroundIsClicked="True"
                     x:Class="Popup.MyPopupPage">
        <popup:PopupPage.Content>
            <StackLayout Padding="10"
                         BackgroundColor="White"
                         HorizontalOptions="Center"
                         VerticalOptions="Center">
                <Label Text="Fresh MVVM Rg.Plugin.Popup"
                    VerticalOptions="CenterAndExpand" 
                    HorizontalOptions="CenterAndExpand" />
                <Button Command="{Binding ClosePopupCommand}"
                        Text="Close Popup"/>
            </StackLayout>
        </popup:PopupPage.Content>
    </popup:PopupPage>
  5. The code behind has “PopupPage” as parent page like shown in the following code part.
    public partial class MyPopupPage : PopupPage
    {
     public MyPopupPage ()
     {
      InitializeComponent ();
     }
    }
  6. Then create a Page Model for the created Popup Page with Fresh MVVM rules. If you have not remember the rules or new to Fresh MVVM, Kindly refer my previous articles on fresh MVVM.
    public class MyPopupPageModel : FreshBasePageModel
    {
    
    }
  7. Rg Popup Page has a separate Navigation Stack. So, open and close the popup, we need to have separate Navigation Stack. To know more about Rg.Plugin.Popup, refer the GitHub Link.
    Rg.Plugin.Popup Link: https://github.com/rotorgames/Rg.Plugins.Popup
  8. Then include Fresh MVVM extension/Utility class to your Xamarin Shared Project. This extension file is created & open sourced by the author “Libin Joseph”. You can download this file from the following GitHub Link.
    Fresh MVVM Extension Link: https://github.com/libin85/FreshMvvm.Popup
  9. To open popup page like navigating to normal Fresh MVVM page, use the following code snippet.
    return new Command(async () =>
    {
            await CoreMethods.PushPopupPageModel();
    });
  10. To close the popup page like closing normal Fresh MVVM page, use the following code snippet.
    return new Command(async () =>
    {
            await CoreMethods.PopPopupPageModel();
    });

Demo:

The below screenshots for your reference.

Main PageRg Popup Page

Reference:

Fresh MVVMhttps://github.com/rid00z/FreshMvvm
Rg.Plugin.Popuphttps://github.com/rotorgames/Rg.Plugins.Popup
Fresh MVVM Popup Extensionhttps://github.com/libin85/FreshMvvm.Popup/

Download Code

You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.