using Microsoft.UI.Composition.SystemBackdrops; using Microsoft.UI.Xaml; using System.Runtime.InteropServices; // For DllImport using WinRT; // required to support Window.As<ICompositionSupportsSystemBackdrop>()
publicsealedpartialclassMainWindow : Window { WindowsSystemDispatcherQueueHelper m_wsdqHelper; // See below for implementation. MicaController m_backdropController; SystemBackdropConfiguration m_configurationSource;
publicMainWindow() { this.InitializeComponent();
TrySetSystemBackdrop(); }
boolTrySetSystemBackdrop() { if (Microsoft.UI.Composition.SystemBackdrops.MicaController.IsSupported()) { m_wsdqHelper = new WindowsSystemDispatcherQueueHelper(); m_wsdqHelper.EnsureWindowsSystemDispatcherQueueController();
// Create the policy object. m_configurationSource = new SystemBackdropConfiguration(); this.Activated += Window_Activated; this.Closed += Window_Closed; ((FrameworkElement)this.Content).ActualThemeChanged += Window_ThemeChanged;
m_backdropController = new Microsoft.UI.Composition.SystemBackdrops.MicaController()
// Enable the system backdrop. // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. m_backdropController.AddSystemBackdropTarget(this.As<Microsoft.UI.Composition.ICompositionSupportsSystemBackdrop>()); m_backdropController.SetSystemBackdropConfiguration(m_configurationSource); returntrue; // succeeded }
returnfalse; // Mica is not supported on this system }
privatevoidWindow_Closed(object sender, WindowEventArgs args) { // Make sure any Mica/Acrylic controller is disposed // so it doesn't try to use this closed window. if (m_backdropController != null) { m_backdropController.Dispose(); m_backdropController = null; } this.Activated -= Window_Activated; m_configurationSource = null; }
object m_dispatcherQueueController = null; publicvoidEnsureWindowsSystemDispatcherQueueController() { if (Windows.System.DispatcherQueue.GetForCurrentThread() != null) { // one already exists, so we'll just use it. return; }