Showing posts with label mopups. Show all posts

Introduction .NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and...

MOPUP (Alternative of Rg.Plugin.Popup) in .NET MAUI MOPUP (Alternative of Rg.Plugin.Popup) in .NET MAUI

A blog about android developement

mopups

Introduction

.NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and XAML. It enables the creation of apps that seamlessly operate on Android, iOS, macOS, and Windows, all from a unified codebase. This open-source platform is an advancement of Xamarin Forms, expanding its reach to desktop scenarios while enhancing UI controls for improved performance and extensibility.

Mopups is a replacement for the "Rg.Plugins.Popups" plugin for Xamarin. Mopups intends to provide a similar experience to this plugin, however also clean up the code base and provide forward looking enhancements.


In this article, we will see how we can see the alternative to Rg.Plugin.Popup - MOPUP Page in .NET MAUI project.

Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • In the Create a new project window, select MAUI in the All project types drop-down, select the .NET MAUI App template, and click the Next button:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Install Plugins:

  • Open Nuget Package Manager. Search MOPUPS and click install to the install the plugins in the projects.

Implementation

Initialize Mopups

In your MauiProgram.cs file, inside the CreateMauiAppBuilder method, include a call to .ConfigureMopups() on the host builder to set up the Mopups library:

using Mopups;
// ... 
public static MauiApp CreateMauiAppBuilder()
{
    var builder = MauiApp.CreateBuilder();
    // Other configurations
    builder.ConfigureMopups();
    builder.Services.AddSingleton<IPopupNavigation>(MopupService.Instance);
    builder.Services.AddTransient<MainPage>();
    return builder.Build();
}

Create a Custom PopupPage

Include a new ContentPage in your project and update the base class of the XAML file to PopupPage. Don't forget to add the required namespace declaration for the Mopups.Pages namespace:
<?xml version="1.0" encoding="utf-8" ?>
<mopup:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:YourNamespace"
                 xmlns:mopup="clr-namespace:Mopups.Pages;assembly=Mopups"
                 x:Class="YourNamespace.YourPopupPage">

    <!-- Your PopupPage content goes here -->

</mopup:PopupPage>

Create and Display the Popup

Open MainPage.xaml.cs add the event to open the popup like below.

public partial class MainPage : ContentPage
{
	IPopupNavigation popupNavigation;

	public MainPage(IPopupNavigation popupNavigation)
	{
		InitializeComponent();

		this.popupNavigation = popupNavigation;
	}

	private void OnCounterClicked(object sender, EventArgs e)
	{
		popupNavigation.PushAsync(new MyPopupPage());
	}
}

Add the following code to close the popup screen

MopupService.Instance.PopAsync();

Full Code:

Demo

Download Code:

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

References

https://github.com/LuckyDucko/Mopups