Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ void HandleShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted(object? s
}
}

[Theory]
[InlineData(unchecked((int)0x8007007E), "Unable to load resource dll. Microsoft.WindowsAppRuntime.Insights.Resource.dll", true)]
[InlineData(unchecked((int)0x8007007E), "Unable to load resource dll. Other.Resource.dll", false)]
[InlineData(unchecked((int)0x80004005), "Unable to load resource dll. Microsoft.WindowsAppRuntime.Insights.Resource.dll", false)]
public void IsWindowsAppRuntimeModuleUnavailableReturnsExpectedResult(int hresult, string message, bool expected)
{
var exception = new System.Runtime.InteropServices.COMException(message, hresult);

Assert.Equal(expected, Options.IsWindowsAppRuntimeModuleUnavailable(exception));
}

[Fact]
public void UseMauiCommunityToolkitMediaElement_ShouldUseSurfaceViewByDefault()
{
Expand Down
29 changes: 26 additions & 3 deletions src/CommunityToolkit.Maui/Options.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace CommunityToolkit.Maui;
/// </summary>
public class Options : Core.Options
{
const string windowsAppRuntimeInsightsResourceDll = "Microsoft.WindowsAppRuntime.Insights.Resource.dll";
const int moduleNotFoundHResult = unchecked((int)0x8007007E);
#if WINDOWS
static bool isSnackbarNotificationManagerRegistered;
#endif
readonly MauiAppBuilder? builder;

internal Options(in MauiAppBuilder builder) : this()
Expand Down Expand Up @@ -86,8 +91,21 @@ public void SetShouldEnableSnackbarOnWindows(bool value)

else if (Application.Current.Windows.Count is 1)
{
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register();
var notificationManager = Microsoft.Windows.AppNotifications.AppNotificationManager.Default;

try
{
notificationManager.Register();
isSnackbarNotificationManagerRegistered = true;
notificationManager.NotificationInvoked += OnSnackbarNotificationInvoked;
}
// Windows App SDK can omit the Insights resource DLL from self-contained unpackaged apps.
// Registration then fails, but notifications can still be shown without action callbacks. See https://github.com/microsoft/WindowsAppSDK/issues/6071.
catch (System.Runtime.InteropServices.COMException exception) when (IsWindowsAppRuntimeModuleUnavailable(exception))
{
System.Diagnostics.Trace.WriteLine(
$"{nameof(Alerts.Snackbar)} action callbacks could not be registered because a Windows App Runtime module is unavailable. Snackbar notifications remain enabled. {exception}");
}
Comment thread
jfversluis marked this conversation as resolved.
}
})
.OnClosed((_, _) =>
Expand All @@ -96,10 +114,11 @@ public void SetShouldEnableSnackbarOnWindows(bool value)
{
throw new InvalidOperationException($"{nameof(Application)}.{nameof(Application.Current)} cannot be null when Windows are closed");
}
else if (Application.Current.Windows.Count is 0)
else if (Application.Current.Windows.Count is 0 && isSnackbarNotificationManagerRegistered)
{
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister();
isSnackbarNotificationManagerRegistered = false;
}
}));
});
Expand Down Expand Up @@ -134,4 +153,8 @@ public void SetPopupOptionsDefaults(DefaultPopupOptionsSettings globalPopupOptio
{
DefaultPopupOptionsSettings = globalPopupOptionsSettings;
}

internal static bool IsWindowsAppRuntimeModuleUnavailable(System.Runtime.InteropServices.COMException exception)
=> exception.HResult is moduleNotFoundHResult
&& exception.Message.Contains(windowsAppRuntimeInsightsResourceDll, StringComparison.OrdinalIgnoreCase);
}
Loading