[Bug] Popup throws exception when called from OnAppearing()
See original GitHub issueDescription
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:
- Created a year ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top GitHub Comments
Works like a charm.
Thanks @pictos @VladislavAntonyuk
You can override OnHandlerChanged. Without subscription OnAppearing