question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Popup throws exception when called from OnAppearing()

See original GitHub issue

Description

We are able to open a Popup control from a command on the ViewModel, as well as from a button clicked event. However, when calling this.ShowPopup(popup) from the ContentPage OnAppearing() method, an exception is thrown as follows:

System.InvalidOperationException: Could not locate MauiContext at CommunityToolkit.Maui.Views.PopupExtensions.CreatePopup(Page page, Popup popup) at CommunityToolkit.Maui.Views.PopupExtensions.ShowPopup[AlertPopup](Page page, AlertPopup popup)

We have only tested Android so far.

Stack Trace

at CommunityToolkit.Maui.Views.PopupExtensions.CreatePopup(Page page, Popup popup) at CommunityToolkit.Maui.Views.PopupExtensions.ShowPopup[AlertPopup](Page page, AlertPopup popup) at MiX.Amaza.Mobile.BaseApp.Pages.Profile.ProfilePage.OnAppearing() in C:\git\Amaza\MiX.Amaza.Mobile.BaseApp\Pages\Profile\ProfilePage.xaml.cs:line 33 at Microsoft.Maui.Controls.Page.SendAppearing() in D:\a\_work\1\s\src\Controls\src\Core\Page.cs:line 485 at Microsoft.Maui.Controls.ShellContent.SendPageAppearing(Page page) in D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:line 150 at Microsoft.Maui.Controls.ShellContent.SendAppearing() in D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:line 131 at Microsoft.Maui.Controls.ShellContent.OnChildAdded(Element child) in D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:line 161 at Microsoft.Maui.Controls.ShellContent.set_ContentCache(Page value) in D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:line 202 at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent() in D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:line 81 at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in D:\a\work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellSectionRenderer.cs:line 122 at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in C:\a\_work\1\s\generated\androidx.fragment.fragment\obj\Release
et6.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:line 1999 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 352

Code

AlertPopup.cs

public class AlertPopup : Popup
	{
		public AlertPopup() : base()
		{
			Content = new VerticalStackLayout
			{
				Children =
				{
						new Label
						{
								Text = "This is a very important message!"
						}
				}
			};
		}
	}

ProfilePage.xaml.cs

public partial class ProfilePage : ContentPage
{
	public ProfilePage() : base()
	{
		InitializeComponent();
	}

	protected override void OnAppearing()
	{
		base.OnAppearing();
		try
		{
			var popup = new AlertPopup();
			// This is where the exception is thrown
			this.ShowPopup(popup);
		}
		catch (Exception ex)
		{
			throw;
		}
	}
}

Basic Information

  • Version with issue: 1.0.0
  • IDE: Microsoft Visual Studio Professional 2022 (64-bit) - Preview Version 17.3.0 Preview 1.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mariusmcalpinecommented, Jun 23, 2022

Works like a charm.

protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();

        var popup = new SimplePopupCS();
        this.ShowPopup(popup);
    }

Thanks @pictos @VladislavAntonyuk

1reaction
VladislavAntonyukcommented, Jun 22, 2022

You can override OnHandlerChanged. Without subscription OnAppearing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Onappearing not working when modal page is popped in ...
I have an onappearing method which doesn't fire when page is popped in Xamarin iOS but works fine in Xamarin Android. Could anyone...
Read more >
[BUG] Popup not showing in NavigatedTo event on Shell.
Do something. Call the Popup's Close method in the NavigateTo event. Popup is not displayed at 3. The following exception occurs at 4....
Read more >
Popup - .NET MAUI Community Toolkit
Close() is a fire-and-forget method. It will complete and return to the calling thread before the operating system has dismissed the Popup from ......
Read more >
Error processing file through intent filter .Net MAUI
Hello. I'm trying to implement functionality for opening and displaying documents through the intent filter and from the application itself.
Read more >
Xamarin Community Forums
Hi, We are using Interstate Bold Custom font in our Xamarin forms. But we are getting an error “throwing exception that “ Font...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found