CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.27k stars 401 forks source link

[BUG] [Windows] The second popup will fail to show after MAUI 8.0.60 #1931

Closed GuidoNeele closed 5 months ago

GuidoNeele commented 5 months ago

Is there an existing issue for this?

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

Current Behavior

This is a very strange issue. Since MAUI version 8.0.60 the second popup will fail. Only the second time you want to show a popup will fail. The popup will not show up and if you are using CommunityToolkit.MVVM [RelayCommand] the button (triggering the popup) will be disabled, because the Task will not return. See video of the problem.

https://github.com/CommunityToolkit/Maui/assets/1301586/0cc33ecd-a70e-40fc-a295-5f2255d4e126

Expected Behavior

The popup should always be shown also the second popup.

Steps To Reproduce

  1. Download latest version of CommunityToolkit
  2. Open Directory.Build.props in root folder of project
  3. Change MauiPackageVersion property to 8.0.60
  4. Open Visual Studio and run the sample application
  5. Go to Views / Multiple Popups Page
  6. Click on "Simple Popup"
  7. Close the popup by clicking outside the popup
  8. Click on "C# Binding Popup"
  9. The button will be disabled and the popup will not appear

Link to public reproduction project repository

https://github.com/CommunityToolkit/Maui

Environment

- .NET MAUI CommunityToolkit: 9.0.1
- OS: Windows 11 (net8.0-windows10.0.19041.0)
- .NET MAUI: 8.0.60

Anything else?

To prevent the button from disabling while using CommunityToolkit.MVVM is to use [RelayCommand(AllowConcurrentExecutions = true)] instead of [RelayCommand]

GuidoNeele commented 5 months ago

Tracked this issue down to the Loaded event not getting triggered, resulting in the taskCompletionSource not completing, which will keep the task running.

https://github.com/CommunityToolkit/Maui/blob/442f39e3de67c4578743fa50b3d59083cea436fd/src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs#L62-L81

Think this issue is caused by this fix https://github.com/dotnet/maui/commit/21620dc32b85003364a497310231ce48d6831ac7 which is included in 9.0.0-preview.5.24307.10 and 8.0.60

This is not an issue on Android. Didn't test on other platforms.

GuidoNeele commented 5 months ago

Issue is fixed if the Unload event is set too.

var taskCompletionSource = new TaskCompletionSource<object?>();

void unloadedHandler(object? sender, EventArgs args) => {};

async void loadedHandler(object? sender, EventArgs args)
{
    page.GetCurrentPage().Unloaded -= unloadedHandler;
    page.GetCurrentPage().Loaded -= loadedHandler;

    try
    {
        var result = await CreateAndShowPopupAsync(page, popup, token);

        taskCompletionSource.TrySetResult(result);
    }
    catch (Exception ex)
    {
        taskCompletionSource.TrySetException(ex);
    }
}

page.GetCurrentPage().Unloaded += unloadedHandler;
page.GetCurrentPage().Loaded += loadedHandler;

return taskCompletionSource.Task.WaitAsync(token);
bijington commented 5 months ago

Any chance the issue relates to this https://github.com/microsoft/microsoft-ui-xaml/issues/1900

PureWeen commented 4 months ago

This fix should be available on the nightly feed as version 8.0.61-ci.net8.24319.3.

Can you test with the latest nightly build? https://github.com/dotnet/maui/wiki/Nightly-Builds

GuidoNeele commented 4 months ago

Hi @PureWeen,

Just tried the nightly build and it indeed fixes the issue. Thanks for the swift fix!