Mopups icon indicating copy to clipboard operation
Mopups copied to clipboard

Support for MAUI Embedding

Open andreas-nesheim opened this issue 1 year ago • 5 comments

SUMMARY Hi! Thank you very much for your work in porting this library to .NET MAUI, it is greatly appreciated!

We have an app that utilizes MAUI Embedding to use MAUI pages in ".NET for Android" and ".NET for iOS" apps (previously Xamarin.Android and Xamarin.iOS). I was hoping to use your library in this app, but it seems that the Mopups registration doesn't seem to take effect when using this approach. Do you know if this is possible at all with this approach, or if this is something that's possible to implement? When I try to open a popup page from the iOS project, I get this error at runtime:

System.NullReferenceException: Object reference not set to an instance of an object. at Mopups.iOS.Implementation.iOSMopups.AddAsync(PopupPage page) at Mopups.Services.PopupNavigation.<>c__DisplayClass20_0.<<PushAsync>g__PushPage|0>d.MoveNext()

On Android I just get a NullReferenceException at runtime.

DETAILS The use-case involves having one class library for the iOS/Android app and one class library where the MAUI pages are located. I use the embedding technique (as described in the previous link) and I use the host builder to add the .ConfigureMopups() in my AppDelegate.cs / MainActivity.cs. I've attached a repro of a project that uses MAUI embedding and that tries to use this package.

I understand that this embedding technique might not be all that used, but it would be great if there is a way to make this package work with this setup.

PLATFORMS

  • [x] Android
  • [x] iOS

MAUIEmbedding.zip

andreas-nesheim avatar Apr 18 '23 11:04 andreas-nesheim

On Android anyway, (iOS probably similar), MauiApplication.Current is null. How does one get this to become initialized or what should one be using instead of this?

page.Parent = MauiApplication.Current.Application.Windows[0].Content as Element;

Apparently Alerts in MAUI have the same issue, so it's probably going to be a race to see who takes on the solution first.

bakerhillpins avatar May 18 '23 14:05 bakerhillpins

I have made some changes to make Mopups work with Maui Embedded. So far so good...

The main change I made was to check if the PopupPage.Parent is null or not before setting it to the MauiApplication.Current..Windows[0].Content

To make it work with Maui Embedded when you create your popup page:

  • Get: https://github.com/ultimategrandson/Mopups
  • In your Activity and/or AppDelegate, make sure to call Microsoft.Maui.ApplicationModel.Platform.Init(...)
  • When you create an instance of your PopupPage, ensure you set Parent to the parent ContentPage.

I also fixed couple issues on iOS, I'm not sure if these were a result of using Maui Embedded or not but,

  • First issue, caused the Window to display with a white background. There is a line of code in the constructor of PopupWindow.cs that set the background to be transparent, that I uncommented and it fixed the issue.
  • Second issue, caused my app to crash and it occurred when I opened and closed the popup repeatedly. The HitTest in the UIWindow is getting raised multiple times and this seems to cause issues when it tries to close the popup. I added the _stop boolean to just prevent anymore calls to SendBackgroundClick.

ultimategrandson avatar Sep 13 '23 03:09 ultimategrandson

Does it could solve the issue that when showing (for example) a loading popup before a navigation page "PushAsync" the popup stays in the backgroud ?

AldairOrta avatar Sep 15 '23 18:09 AldairOrta

Nope. It looks like if you use Maui Embedded anything relating to navigation, like pushing/popping views and events like Appearing/Disappearing either don't work properly or not at all.

To fix those issues, you will have to implement it yourself, using MaterialAlertDialogBuilder on android or UIAlertController on iOS.

ultimategrandson avatar Sep 17 '23 01:09 ultimategrandson

@ultimategrandson

Just clocking in noticed your work, i should make another release with this in the mix

LuckyDucko avatar Oct 09 '23 06:10 LuckyDucko