diff --git a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs index c2fdd59dce..53ee7bfb68 100644 --- a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs +++ b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs @@ -41,10 +41,14 @@ public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder() #if DEBUG - .UseMauiCommunityToolkit() + .UseMauiCommunityToolkit(options => + { + options.SetShouldEnableSnackbarOnWindows(true); + }) #else .UseMauiCommunityToolkit(options => { + options.SetShouldEnableSnackbarOnWindows(true); options.SetShouldSuppressExceptionsInConverters(true); options.SetShouldSuppressExceptionsInBehaviors(true); options.SetShouldSuppressExceptionsInAnimations(true); diff --git a/src/CommunityToolkit.Maui/Alerts/Snackbar/Snackbar.shared.cs b/src/CommunityToolkit.Maui/Alerts/Snackbar/Snackbar.shared.cs index 2a0097d20c..71b92c8ea9 100644 --- a/src/CommunityToolkit.Maui/Alerts/Snackbar/Snackbar.shared.cs +++ b/src/CommunityToolkit.Maui/Alerts/Snackbar/Snackbar.shared.cs @@ -16,6 +16,16 @@ public partial class Snackbar : ISnackbar /// public Snackbar() { +#if WINDOWS + if (!Options.ShouldEnableSnackbarOnWindows) + { + throw new InvalidOperationException($"Additional setup is required in the Package.appxmanifest file to enable {nameof(Snackbar)} on Windows. Additonally, `{nameof(AppBuilderExtensions.UseMauiCommunityToolkit)}(options => options.{nameof(Options.SetShouldEnableSnackbarOnWindows)}({bool.TrueString.ToLower()});` must be called to enable Snackbar on Windows. See the Platform Specific Initialization section of the {nameof(Snackbar)} documentaion for more information: https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar") + { + HelpLink = "https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar" + }; + } +#endif + Duration = GetDefaultTimeSpan(); VisualOptions = new SnackbarOptions(); } diff --git a/src/CommunityToolkit.Maui/AppBuilderExtensions.shared.cs b/src/CommunityToolkit.Maui/AppBuilderExtensions.shared.cs index 9f522f2bba..56003290bf 100644 --- a/src/CommunityToolkit.Maui/AppBuilderExtensions.shared.cs +++ b/src/CommunityToolkit.Maui/AppBuilderExtensions.shared.cs @@ -23,27 +23,10 @@ public static MauiAppBuilder UseMauiCommunityToolkit(this MauiAppBuilder builder // Pass `null` because `options?.Invoke()` will set options on both `CommunityToolkit.Maui` and `CommunityToolkit.Maui.Core` builder.UseMauiCommunityToolkitCore(null); -#if WINDOWS - builder.ConfigureLifecycleEvents(events => - { - events.AddWindows(windows => windows - .OnLaunched((_, _) => - { - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked; - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register(); - }) - .OnClosed((_, _) => - { - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked; - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister(); - })); - }); -#endif - builder.Services.AddSingleton(); // Invokes options for both `CommunityToolkit.Maui` and `CommunityToolkit.Maui.Core` - options?.Invoke(new Options()); + options?.Invoke(new Options(builder)); builder.ConfigureMauiHandlers(h => { @@ -56,12 +39,4 @@ public static MauiAppBuilder UseMauiCommunityToolkit(this MauiAppBuilder builder NavigationBar.RemapForControls(); return builder; } -#if WINDOWS - static void OnSnackbarNotificationInvoked( - Microsoft.Windows.AppNotifications.AppNotificationManager sender, - Microsoft.Windows.AppNotifications.AppNotificationActivatedEventArgs args) - { - Snackbar.HandleSnackbarAction(args); - } -#endif } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui/Options.cs b/src/CommunityToolkit.Maui/Options.cs index 055e14796c..6199182544 100644 --- a/src/CommunityToolkit.Maui/Options.cs +++ b/src/CommunityToolkit.Maui/Options.cs @@ -1,32 +1,91 @@ using CommunityToolkit.Maui.Behaviors; using CommunityToolkit.Maui.Converters; +using Microsoft.Maui.LifecycleEvents; namespace CommunityToolkit.Maui; /// /// .NET MAUI Community Toolkit Options. /// -public class Options : Core.Options +public class Options() : Core.Options { + readonly MauiAppBuilder? builder; + + internal Options(in MauiAppBuilder builder) : this() + { + this.builder = builder; + } + internal static bool ShouldSuppressExceptionsInAnimations { get; private set; } internal static bool ShouldSuppressExceptionsInConverters { get; private set; } internal static bool ShouldSuppressExceptionsInBehaviors { get; private set; } + internal static bool ShouldEnableSnackbarOnWindows { get; private set; } /// /// Allows to return default value instead of throwing an exception when using . - /// Default value is false. /// + /// + /// Default value is false. + /// public void SetShouldSuppressExceptionsInConverters(bool value) => ShouldSuppressExceptionsInConverters = value; /// /// Allows to return default value instead of throwing an exception when using . - /// Default value is false. /// + /// + /// Default value is false. + /// public void SetShouldSuppressExceptionsInAnimations(bool value) => ShouldSuppressExceptionsInAnimations = value; /// /// Allows to return default value instead of throwing an exception when using . - /// Default value is false. /// + /// + /// Default value is false. + /// public void SetShouldSuppressExceptionsInBehaviors(bool value) => ShouldSuppressExceptionsInBehaviors = value; + + /// + /// Enables for Windows + /// + /// + /// Additional setup is required in the Package.appxmanifest file to enable on Windows. See the Snackbar Platform Specific Initialization Documentation for more information. Default value is false. + /// + public void SetShouldEnableSnackbarOnWindows(bool value) + { +#if WINDOWS + if (value is true && builder is null) + { + throw new InvalidOperationException($"{nameof(SetShouldEnableSnackbarOnWindows)} must be called using the {nameof(AppBuilderExtensions.UseMauiCommunityToolkit)} extension method. See the Platform Specific Initialization section of the {nameof(Alerts.Snackbar)} documentaion for more inforamtion: https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar)") + { + HelpLink = "https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar" + }; + } + else if (value is true && builder is not null) + { + builder.ConfigureLifecycleEvents(events => + { + events.AddWindows(windows => windows + .OnLaunched((_, _) => + { + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked; + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register(); + }) + .OnClosed((_, _) => + { + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked; + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister(); + })); + }); + + static void OnSnackbarNotificationInvoked(Microsoft.Windows.AppNotifications.AppNotificationManager sender, + Microsoft.Windows.AppNotifications.AppNotificationActivatedEventArgs args) + { + Alerts.Snackbar.HandleSnackbarAction(args); + } + } +#endif + + ShouldEnableSnackbarOnWindows = value; + } } \ No newline at end of file