Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
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 .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.40Anything else?
I can't upgrade to 9.0.50 because of #2583
Activity
bijington commentedon Mar 30, 2025
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?
Ebola-Chan-bot commentedon Mar 31, 2025
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.
pictos commentedon Mar 31, 2025
@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
bijington commentedon Mar 31, 2025
Sorry I thought we had included this in the documentation! I can see that we can get it added
stephenquan commentedon Apr 7, 2025
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.
Ebola-Chan-bot commentedon Apr 8, 2025
@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.
pictos commentedon Apr 8, 2025
@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