Skip to content

[BUG] Popup doesn't close properly after the second show #2607

Closed

Description

@Ebola-Chan-bot

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

Popup doesn't close properly after the second show-close cycle. The window becomes unusable.

Expected Behavior

The popup should be able to click on an empty space to close gracefully, and then I should be able to use the other controls in the window, no matter how many times it is repeated.

Steps To Reproduce

Create a new MAUI app project named MauiApp2 in Visual Studio.

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
			 xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="MauiApp2.MainPage">
	<ContentPage.Resources>
		<toolkit:Popup x:Key="PU" x:Name="PU">
			<Image/>
		</toolkit:Popup>
	</ContentPage.Resources>

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Image
                Source="dotnet_bot.png"
                HeightRequest="185"
                Aspect="AspectFit"
                SemanticProperties.Description="dot net bot in a hovercraft number nine" />

            <Label
                Text="Hello, World!"
                Style="{StaticResource Headline}"
                SemanticProperties.HeadingLevel="Level1" />

            <Label
                Text="Welcome to &#10;.NET Multi-platform App UI"
                Style="{StaticResource SubHeadline}"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I" />

            <Button
                x:Name="CounterBtn"
                Text="Click me" 
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Fill" />
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

MainPage.xaml.cs:

using CommunityToolkit.Maui.Views;

namespace MauiApp2
{
	public partial class MainPage : ContentPage
	{
		int count = 0;

		public MainPage()
		{
			InitializeComponent();
		}

		private void OnCounterClicked(object sender, EventArgs e)
		{
			this.ShowPopup(PU);
		}
	}

}

MauiProgram.cs:

using CommunityToolkit.Maui;
using Microsoft.Extensions.Logging;

namespace MauiApp2
{
	public static class MauiProgram
	{
		public static MauiApp CreateMauiApp()
		{
			var builder = MauiApp.CreateBuilder();
			builder
				.UseMauiApp<App>()
			.UseMauiCommunityToolkit()
				.ConfigureFonts(fonts =>
				{
					fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
					fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
				});

#if DEBUG
			builder.Logging.AddDebug();
#endif

			return builder.Build();
		}
	}
}

Run the app on Windows. Click on the button to display the Popup. Click on any blank space and the Popup closes normally. Click the button again and the Popup will be displayed again. Click on any empty space again, and the Popup fails to close properly.

Link to public reproduction project repository

https://github.com/Ebola-Chan-bot/MirageTank/tree/master/MauiApp2

Environment

- .NET MAUI CommunityToolkit:11.1.0
- OS: Windows 11 Build 10.0.27823.1000
- .NET MAUI: 9.0.40

Anything else?

I can't upgrade to 9.0.50 because of #2583

Activity

  1. bijington commented on Mar 30, 2025

    @bijington
    Contributor

    This approach is currently not supported. You should not show the same Popup instance more than once

    @VladislavAntonyuk do you know if this limitation will change with Popup V2?

  2. Ebola-Chan-bot commented on Mar 31, 2025

    @Ebola-Chan-bot
    Author

    This approach is currently not supported. You should not show the same Popup instance more than once

    @VladislavAntonyuk do you know if this limitation will change with Popup V2?

    It was a surprise to me that such a control couldn't be reused. If this is by design, should you consider noting it in the documentation? I didn't find a description about this limitation in the documentation.

  3. pictos commented on Mar 31, 2025

    @pictos
    Member

    @Ebola-Chan-bot it's by design, in early implementations of our controls we were very aggressive in disposing off controls as soon they leave the visual tree, to avoid memory leaks.

    You're right, it's worth to mention that on docs, but since we plan to deprecate the current implementation not sure if that will add some value.

    @bijington AFAIK for net 9 devs can choose if handlers should be disconnected as soon the control leaves the visual tree or not. So I think it will solve this issue

  4. bijington commented on Mar 31, 2025

    @bijington
    Contributor

    Sorry I thought we had included this in the documentation! I can see that we can get it added

  5. stephenquan commented on Apr 7, 2025

    @stephenquan
    Contributor

    It is actually inferred in the documentation, since if you look at https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/popup#presenting-a-popup it is clearly shows a new popup instance every time, i.e.

    using CommunityToolkit.Maui.Views;
    
    public class MyPage : ContentPage
    {
        public void DisplayPopup()
        {
            var popup = new SimplePopup();
    
            this.ShowPopup(popup);
        }
    }
  6. Ebola-Chan-bot commented on Apr 8, 2025

    @Ebola-Chan-bot
    Author

    @stephenquan I can hardly agree with the validity of such inference. This looks a lot like a hurried code snippet written on the fly but without performance in mind.

  7. pictos commented on Apr 8, 2025

    @pictos
    Member

    This looks a lot like a hurried code snippet written on the fly but without performance in mind.

    @Ebola-Chan-bot actually the current implementation has performance in mind, but isn't that clear... You know, the handler arch on Maui didn't clean-up itself aggressively before .net 9, which caused a lot of memory leak scenarios because devs didn't know a good way to control the Controls life cycle.

    In order to avoid that we aggressively clean up popups and - if I'm not wrong - MediaElement as well, since these controls hold a lot of native references and could cause a massive memory leak in managed and unmanaged world.

    And this is a good reminder for us to revisit the clean-up strategy, now that Maui has a better story for that

  8. 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