Skip to content

[BUG] Exception when opening a popup from modal on Windows #2627

Closed

Description

@lenardg

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

If I have customized the Flyout Header of my Shell (added Shell.FlyoutHeader to my Shell), then when opening a Popup inside a Modal on Windows, I get an exception. It works on Android. It works without the Modal. It works without the flyout header customization.

This seems very similar to an existing issue #2459 , however, I can toggle the error on and off by removing the flyout header from my shell.

Exception: System.Runtime.InteropServices.COMException

Message: : 'No installed components were detected.
'

Stack trace:

at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
   at WinRT.DelegateExtensions.DynamicInvokeAbi(Delegate del, Object[] invoke_params)
   at ABI.System.Collections.Generic.IListMethods`2.AppendDynamic(IObjectReference obj, T value)
   at ABI.System.Collections.Generic.IVectorMethods`1.Append(IObjectReference obj, T value)
   at ABI.System.Collections.Generic.IListMethods`1.Add(IObjectReference obj, T item)
   at Microsoft.UI.Xaml.Controls.UIElementCollection.Add(UIElement item)
   at Microsoft.Maui.Controls.Platform.ViewToHandlerConverter.WrapperControl..ctor(View view)
   at Microsoft.Maui.Controls.Platform.ShellHeaderView.UpdateHeader()
   at Microsoft.Maui.Controls.Platform.ShellHeaderView.SetElement(Shell shell)
   at Microsoft.Maui.Controls.Platform.ShellHeaderView..ctor(Shell element)
   at Microsoft.Maui.Controls.Handlers.ShellHandler.MapFlyoutHeader(ShellHandler handler, Shell view)
   at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0.<Add>b__0(IElementHandler h, IElement v)
   at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
   at Microsoft.Maui.Handlers.ViewHandler`2.SetVirtualView(IView view)
   at Microsoft.Maui.Controls.Handlers.ShellHandler.SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2.SetVirtualView(IElement view)
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
   at CommunityToolkit.Maui.Core.Handlers.PopupHandler.MapOnOpened(PopupHandler handler, IPopup view, Object result)
   at Microsoft.Maui.CommandMapper`2.<>c__DisplayClass6_0.<Add>b__0(IElementHandler h, IElement v, Object o)
   at Microsoft.Maui.CommandMapper.InvokeCore(String key, IElementHandler viewHandler, IElement virtualView, Object args)
   at Microsoft.Maui.CommandMapper.Invoke(IElementHandler viewHandler, IElement virtualView, String property, Object args)
   at Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args)
   at CommunityToolkit.Maui.Views.PopupExtensions.PlatformShowPopup(Popup popup, IMauiContext mauiContext)
   at CommunityToolkit.Maui.Views.PopupExtensions.CreateAndShowPopup[TPopup](Page page, TPopup popup)
   at CommunityToolkit.Maui.Views.PopupExtensions.<>c__DisplayClass0_0`1.<ShowPopup>g__handler|0(Object sender, EventArgs args)
   at Microsoft.Maui.Controls.VisualElement.SendLoaded(Boolean updateWiring)
   at Microsoft.Maui.Controls.VisualElement.HandlePlatformUnloadedLoaded()
   at Microsoft.Maui.Controls.VisualElement.UpdatePlatformUnloadedLoadedWiring(Window newWindow, Window oldWindow)
   at Microsoft.Maui.Controls.VisualElement.add_Loaded(EventHandler value)
   at CommunityToolkit.Maui.Views.PopupExtensions.ShowPopup[TPopup](Page page, TPopup popup)
   at MauiApp742.AnotherModalPage.Button_Clicked(Object sender, EventArgs e) in D:\Devel\X\MauiApp742\MauiApp742\AnotherModalPage.xaml.cs:line 14
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked()
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
   at Microsoft.Maui.Controls.Button.SendClicked()
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked()
   at Microsoft.Maui.Handlers.ButtonHandler.OnClick(Object sender, RoutedEventArgs e)
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

Expected Behavior

Popup would open without an Exception. 🤷🏻‍♂️

Steps To Reproduce

Repro steps:

  1. Add a new ContentPage derived page (I used XAML+C#) to the project
  2. Add a Button to MainPage that uses PushModalAsync() to move to this new page
  3. Add an empty or simple Popup to the project.
  4. Add a Button to the new ContentPage that uses ShowPopup() to show the popup.
  5. On opening the popup, it works.
  6. Add the following code to your AppShell.xaml:
    <Shell.FlyoutHeader>
        <StackLayout Orientation="Vertical" HorizontalOptions="Center" Margin="10,10,10,20">
            <Label Text="Some example data"/>
            <Label Text="Some more data"/>
        </StackLayout>
    </Shell.FlyoutHeader>
  1. Now try to open the popup from the modal, it should throw the exception.

Link to public reproduction project repository

https://github.com/lenardg/maui-communitytoolkit-bugrepro-742

Environment

- .NET MAUI CommunityToolkit: 11.2.0
- OS: Windows 11 24H2 (26100.3775), but tested on multiple OS versions including Windows 10, fails every time
- .NET MAUI: 9.0.50

Anything else?

My example uses Locked Shell.FlyoutBehavior for the Shell Flyout, but the bug happens also with the Flyout behavior. I used Locked because in our app that is how it was used, but that does not seem to have any effect on this issue.

Activity

  1. lenardg commented on Apr 11, 2025

    @lenardg
    Author

    @TheCodeTraveler any idea when Popup V2 will land in the released version? This issue kind of blocks our entire .NET 9 migration (from .NET 8) at the moment. 😅

  2. cat0363 commented on Apr 16, 2025

    @cat0363
    Contributor

    I got it to work by modifying the code as follows:

    [src\CommunityToolkit.Maui.Core\Handlers\Popup\PopUpHandler.windows.cs]

    public static void MapOnOpened(PopupHandler handler, IPopup view, object? result)
    {
    	ArgumentNullException.ThrowIfNull(view.Parent);
    	ArgumentNullException.ThrowIfNull(handler.MauiContext);
    
    	if (view.Parent.Handler is not null && view.Parent.Handler.PlatformView is not null)
    	{
    		var parent = view.Parent.Handler.PlatformView;
    		((UIElement)parent).IsHitTestVisible = false;
    	}
    	/* Before modification
    	var parent = view.Parent.ToPlatform(handler.MauiContext);
    	parent.IsHitTestVisible = false;
    	*/
    
    	handler.PlatformView.XamlRoot = view.GetWindow().Content?.Handler?.MauiContext?.GetPlatformWindow().Content.XamlRoot ?? throw new InvalidOperationException("Window Content cannot be null");
    	handler.PlatformView.IsHitTestVisible = true;
    	handler.PlatformView.IsOpen = true;
    
    	AddOverlayToWindow(view.GetWindow());
    
    	view.OnOpened();
    }
    
    protected override void DisconnectHandler(Popup platformView)
    {
    	if (VirtualView.Parent is null)
    	{
    		return;
    	}
    
    	ArgumentNullException.ThrowIfNull(VirtualView.Handler?.MauiContext);
    	if (VirtualView.Parent.Handler is not null && VirtualView.Parent.Handler.PlatformView is not null)
    	{
    		var parent = VirtualView.Parent.Handler.PlatformView;
    		((UIElement)parent).IsHitTestVisible = true;
    	}
    	/* Before modification
    	var parent = VirtualView.Parent.ToPlatform(VirtualView.Handler.MauiContext);
    	parent.IsHitTestVisible = true;
    	*/
    
    	platformView.IsOpen = false;
    	platformView.Closed -= OnClosed;
    	if (MauiContext is not null)
    	{
    		MauiContext.GetPlatformWindow().SizeChanged -= OnSizeChanged;
    	}
    }
    

    The commented out parts are the code before the modification.

    MAUI.App.742.2025-04-16.16-35-22.mp4

    I'm looking forward to the release of Popup V2.

  3. locked and limited conversation to collaborators on May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions