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.25k stars 390 forks source link

Application crashes upon closing a second window when using Builder.options.SetShouldEnableSnackbarOnWindows(true) #2279

Open McSaverInvestments opened 2 weeks ago

McSaverInvestments commented 2 weeks ago

Describe the bug

I am trying to use SnackBar in my application. My application supports multiple windows.
There appears to be an issue upon closing a second windows.

Here is a link to the repo: https://github.com/McSaverInvestments/TestToolKit2

Steps to reproduce

*******    Steps to set up Test MAUI Application which demostrates the bug   ***********************
1)   Inside of visual studio(Version 17.12.0 Preview 2.1) create a new .Net Maui App called TestToolKit2
        Note: The Preview version of visual studio is irrelevant
        Using .NET 8 long term support
2)   Add a Dependency for the CommunityToolkit.MAUI to the new project
3)   To support SnackBar, Modify the Package.appxmaniest file in the Platforms.Windows folder 
        a)  Change Line 7 to the following 
                IgnorableNamespaces="uap rescap com desktop">

        b)  Add the following after (Line 6 and before Line 7)
               xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
               xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"

        c)  Add the following after Line 40 (</uap:VisualElements>)
              <Extensions>
                <!-- Specify which CLSID to activate when notification is clicked -->
                <desktop:Extension Category="windows.toastNotificationActivation">
                  <desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" />
                </desktop:Extension>

                <!-- Register COM CLSID -->
                <com:Extension Category="windows.comServer">
                  <com:ComServer>
                    <com:ExeServer Executable="TestToolKit2\TestToolKit2.exe" DisplayName="$targetnametoken$" Arguments="----AppNotificationActivated:">
                      <com:Class Id="6e919706-2634-4d97-a93c-2213b2acc334" />
                    </com:ExeServer>
                  </com:ComServer>
                </com:Extension>
              </Extensions>
4)   Add the following to MauiProgram.cs after the builder's .UseMauiApp<App>() line of code (Line 12)
                    .UseMauiCommunityToolkit(options =>
                    {
                        // BUG:   Application crashes when a true is passed
                        options.SetShouldEnableSnackbarOnWindows(true);
                    })

        NOTE:  don't forget to add the following to the top of MauiProgram.cs
                    using CommunityToolkit.Maui;
4)   Add the following button to MainPage.xaml (on Line 33)
            <Button
               x:Name="OpenWndBtn"
               Text="Open Window" 
               SemanticProperties.Hint="Open a window when you click"
               Clicked="OnOpenWndClicked"
               HorizontalOptions="Fill" />
5)   Add the following code to MainPage.xaml.cs (after Line 11)
            private void OnOpenWndClicked(object sender, EventArgs e)
            {
                TestWindow.OpenNewWindow(new TestWindow());
            }
6)   Add a new item (TestWindow.cs) to the project and replace the code with the following
        namespace TestToolKit2
        {
            internal class TestWindow : Window
            {
                #region Static -  Methods
                public static void OpenNewWindow<TWindow>(TWindow aWindow)
                     where TWindow : Window
                {
                    Application.Current!.OpenWindow(aWindow);
                } 
                #endregion

                public TestWindow()
                {
                    Page = new TestPage();
                }
            }
        }
7)   Add a new .NET MAUI ContentPage (XAML) called TestPage.xaml to the project
        NOTE: the default generated code supplied by Visual Studio is fine.

//    ******   Steps to produce the error   *********************
//
//   Note:   Using true in the follow line of CreateMauiApp() causes the error to occurs
//               options.SetShouldEnableSnackbarOnWindows(true);
//
// Steps:
//      1)   Start application (Platform:  Windows Machine)
//      2)   Click button that open an additional Window
//      3)   Close that new window
//      4)   Click button that open an second addition Window
//      5)   Close that new window   ( Error seen below occurs on closing second window )

Expected behavior

Windows should close without abort the application!

Here is the call stack: at WinRT.ExceptionHelpers.gThrow|39_0(Int32 hr) at ABI.Microsoft.Windows.AppNotifications.IAppNotificationManagerMethods.Unregister(IObjectReference _obj) at Microsoft.Windows.AppNotifications.AppNotificationManager.Unregister() at CommunityToolkit.Maui.Options.<>c.b224(Window , WindowEventArgs _) at Microsoft.Maui.MauiWinUIWindow.<>cDisplayClass10_0.b0(OnClosed del) at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[TDelegate](IServiceProvider services, Action`1 action) at Microsoft.Maui.MauiWinUIWindow.OnClosed(Object sender, WindowEventArgs args) at Microsoft.Maui.MauiWinUIWindow.OnClosedPrivate(Object sender, WindowEventArgs args) at WinRT._EventSource_globalWindows_Foundation_TypedEventHandler_objectglobalMicrosoft_UI_XamlWindowEventArgs.EventState.b1_0(Object sender, WindowEventArgs args) at ABI.Windows.Foundation.TypedEventHandler`2.Do_Abi_Invoke[TSenderAbi,TResultAbi](Void* thisPtr, TSenderAbi sender, TResultAbi args)

Screenshots

No response

Code Platform

Windows Build Number

Other Windows Build number

Windows 11 10.0.26100 (Build 26100)

App minimum and target SDK version

Other SDK version

10.0.19041.0

Visual Studio Version

2022, Preview

Visual Studio Build Number

Microsoft Visual Studio Community 2022 (ARM 64-bit) - Version 17.12.0 Preview 2.1

Device form factor

Desktop

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item.

### Tasks
dotnet-policy-service[bot] commented 1 week ago

Hi @McSaverInvestments. We have added the "needs reproduction" label to this issue, which indicates that we cannot take further action. This issue will be closed automatically in 5 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

McSaverInvestments commented 1 week ago

Hello Maui Toolkit,

I gave a very detail work flow so you can be able to reproduce the error, please let me know if you need anything more or if you need a link to a repo to see the error in action.

Thanks McSaverInvestments

On Monday, October 14, 2024, dotnet-policy-service[bot] < @.***> wrote:

Hi @McSaverInvestments https://github.com/McSaverInvestments. We have added the "needs reproduction" label to this issue, which indicates that we cannot take further action. This issue will be closed automatically in 5 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

— Reply to this email directly, view it on GitHub https://github.com/CommunityToolkit/Maui/issues/2279#issuecomment-2411820155, or unsubscribe https://github.com/notifications/unsubscribe-auth/AY66BRDAY5JW67KYI26K44LZ3P3WPAVCNFSM6AAAAABP5MRH4GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMJRHAZDAMJVGU . You are receiving this because you were mentioned.Message ID: @.***>

brminnick commented 1 week ago

please let me know if you need anything more or if you need a link to a repo to see the error in action

Yes, thank you. Please edit your Description of the bug and add a link to an open source repository containing a reproduction sample.

Here is more information from ourContributing.md:

Contributing.md

https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug

Image

McSaverInvestments commented 1 week ago

I have edited my original post and added the a link to the repo, here in this message too.

https://github.com/McSaverInvestments/TestToolKit2

Thanks.

@brminnick