在 Windows 11 的桌面设备中应用云母或亚克力材料
Windows 11 中的材料是应用于 UX 图面(类似于真实的手工制品)的视觉效果。 遮挡材料(例如云母和亚克力)用作交互式 UI 控件下的基层。
云母是一种不透明的材料,它将用户的主题和桌面壁纸相结合,创建高度个性化的外观。 云母材料专为性能设计,因为它只捕获一次背景壁纸来创建其可视化效果,因此建议将其用于应用的基础层,特别是在标题栏区域中。
亚克力是一种复制毛玻璃效果的半透明材料。 它仅用于暂时性的轻型消除图面,例如浮出控件和上下文菜单。
本文介绍如何将云母或亚克力材料用作 Windows 应用 SDK/WinUI 3 XAML 应用的基层。
注意
- 若要使用应用内 AcrylicBrush,请参阅 Acrylic 材料。
- 若要在 Win32 应用中使用背景材料,请参阅在 Windows 11 的 Win32 桌面应用中使用云母效果。
- 若要在 UWP/WinUI 2 应用中使用背景材料,请参阅 将 Mica 与 WinUI 2 一起应用于 UWP 或 Acrylic 材料。
如何使用背景材料
WinUI 3 库应用包括大多数 WinUI 3 控件、特性和功能的交互式示例。 通过 Microsoft Store 获取应用,或在 GitHub 上获取源代码
若要将 Mica 或 Acrylic 材料应用于应用,请将该属性设置为 SystemBackdrop
XAML SystemBackdrop(通常为内置背景之一、MicaBackdrop 或 DesktopAcrylicBackdrop)。
这些元素具有属性 SystemBackdrop
:
- CommandBarFlyoutCommandBar.SystemBackdrop
- ContentIsland.SystemBackdrop
- DesktopWindowXamlSource.SystemBackdrop
- FlyoutBase.SystemBackdrop
- MenuFlyoutPresenter.SystemBackdrop
- Popup.SystemBackdrop
- Window.SystemBackdrop
这些示例演示如何在 XAML 和代码中设置系统背景。
Mica
Mica 通常用作应用窗口的背景。
<Window
... >
<Window.SystemBackdrop>
<MicaBackdrop Kind="BaseAlt"/>
</Window.SystemBackdrop>
</Window>
public MainWindow()
{
this.InitializeComponent();
SystemBackdrop = new MicaBackdrop()
{ Kind = MicaKind.BaseAlt };
}
Acrylic
亚克力通常用作暂时性 UI 的背景,如浮出控件。
<Flyout
... >
<Flyout.SystemBackdrop>
<DesktopAcrylicBackdrop/>
</Flyout.SystemBackdrop>
</Flyout>
Flyout flyout = new Flyout()
{
SystemBackdrop = new DesktopAcrylicBackdrop()
};
如何使用系统背景控制器
注意
从 Windows 应用 SDK 1.3 开始,可通过将 Window.SystemBackdrop
属性设置为 XAML SystemBackdrop
来应用材料,如上一部分所述。 这是推荐的材料应用方法。
本文的其余部分介绍如何使用 Composition MicaController 和 DesktopAcrylicController API。
若要在应用中使用背景材料,可使用实现 ISystemBackdropController 接口的控制器之一(MicaController 或 DesktopAcrylicController)。 这些类既管理系统背景材料的呈现,还对材料系统策略的处理进行管理。
若要将云母用作背景材料,请创建 MicaController 对象。 若要使用亚克力,请创建 DesktopAcrylicController 对象。 对于每种类型的系统背景材料,设置和支持代码是相同的。
此代码演示如何创建 MicaController
。
MicaController m_backdropController;
bool TrySetSystemBackdrop()
{
if (MicaController.IsSupported())
{
...
m_backdropController = new MicaController();
...
}
}
// namespace MUCSB = Microsoft::UI::Composition::SystemBackdrops;
winrt::MUCSB::MicaController m_backdropController{ nullptr };
void SetBackground()
{
if (winrt::MUCSB::MicaController::IsSupported())
{
...
m_backdropController = winrt::MUCSB::MicaController();
...
}
}
若要使用云母材料的云母 Alt 变体,请创建 一个 MicaController
对象并将 Kind 属性设置为 MicaKind.BaseAlt。
MicaController m_backdropController;
bool TrySetSystemBackdrop()
{
if (MicaController.IsSupported())
{
...
m_backdropController = new MicaController()
{
Kind = MicaKind.BaseAlt
};
...
}
}
// namespace MUCSB = Microsoft::UI::Composition::SystemBackdrops;
winrt::MUCSB::MicaController m_backdropController{ nullptr };
void SetBackground()
{
if (winrt::MUCSB::MicaController::IsSupported())
{
...
m_backdropController = winrt::MUCSB::MicaController();
m_backdropController.Kind(winrt::MUCSB::MicaKind::BaseAlt);
...
}
}
此代码演示如何创建 DesktopAcrylicController
。
DesktopAcrylicController m_backdropController;
bool TrySetSystemBackdrop()
{
if (DesktopAcrylicController.IsSupported())
{
...
m_backdropController = new DesktopAcrylicController();
...
}
}
// namespace MUCSB = Microsoft::UI::Composition::SystemBackdrops;
winrt::MUCSB::DesktopAcrylicController m_backdropController{ nullptr };
void SetBackground()
{
if (winrt::MUCSB::DesktopAcrylicController::IsSupported())
{
...
m_backdropController = winrt::MUCSB::DesktopAcrylicController();
...
}
}
默认情况下,控制器对系统的浅色和深色主题作出反应。 若要替代此行为,可将以下属性传递给控制器:
若要在应用中使用背景材料,需要以下项:
系统支持
运行应用的系统必须支持背景材料。 调用 MicaController.IsSupported 或 DesktopAcrylicController.IsSupported 方法来确保背景材料在运行时受到支持。
有效目标
必须提供能实现 ICompositionSupportsSystemBackdrop 接口的目标。 在 XAML 应用中,XAML 窗口可实现此接口并用作背景目标。
A SystemBackdropConfiguration 对象
SystemBackdropConfiguration 为系统背景控制器提供特定于应用的策略信息,便于正确配置系统背景材料。
DispatcherQueue 对象。
主 XAML 线程上需要有可用的 Windows.System.DispatcherQueue。 请查看示例代码或 WinUI 3 库示例中的
WindowsSystemDispatcherQueueHelper
类。
示例:在 Windows 应用 SDK/WinUI 3 应用中使用云母效果
此示例演示如何在 XAML 应用中设置云母背景材料。
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Xaml;
using System.Runtime.InteropServices; // For DllImport
using WinRT; // required to support Window.As<ICompositionSupportsSystemBackdrop>()
public sealed partial class MainWindow : Window
{
WindowsSystemDispatcherQueueHelper m_wsdqHelper; // See below for implementation.
MicaController m_backdropController;
SystemBackdropConfiguration m_configurationSource;
public MainWindow()
{
this.InitializeComponent();
TrySetSystemBackdrop();
}
bool TrySetSystemBackdrop()
{
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;
// Initial configuration state.
m_configurationSource.IsInputActive = true;
SetConfigurationSourceTheme();
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);
return true; // succeeded
}
return false; // Mica is not supported on this system
}
private void Window_Activated(object sender, WindowActivatedEventArgs args)
{
m_configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated;
}
private void Window_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;
}
private void Window_ThemeChanged(FrameworkElement sender, object args)
{
if (m_configurationSource != null)
{
SetConfigurationSourceTheme();
}
}
private void SetConfigurationSourceTheme()
{
switch (((FrameworkElement)this.Content).ActualTheme)
{
case ElementTheme.Dark: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Dark; break;
case ElementTheme.Light: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Light; break;
case ElementTheme.Default: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Default; break;
}
}
}
class WindowsSystemDispatcherQueueHelper
{
[StructLayout(LayoutKind.Sequential)]
struct DispatcherQueueOptions
{
internal int dwSize;
internal int threadType;
internal int apartmentType;
}
[DllImport("CoreMessaging.dll")]
private static extern int CreateDispatcherQueueController([In] DispatcherQueueOptions options, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object dispatcherQueueController);
object m_dispatcherQueueController = null;
public void EnsureWindowsSystemDispatcherQueueController()
{
if (Windows.System.DispatcherQueue.GetForCurrentThread() != null)
{
// one already exists, so we'll just use it.
return;
}
if (m_dispatcherQueueController == null)
{
DispatcherQueueOptions options;
options.dwSize = Marshal.SizeOf(typeof(DispatcherQueueOptions));
options.threadType = 2; // DQTYPE_THREAD_CURRENT
options.apartmentType = 2; // DQTAT_COM_STA
CreateDispatcherQueueController(options, ref m_dispatcherQueueController);
}
}
}
// pch.h
...
#include <winrt/Microsoft.UI.Composition.SystemBackdrops.h>
#include <winrt/Windows.System.h>
#include <dispatcherqueue.h>
// MainWindow.xaml.h
...
namespace winrt
{
namespace MUC = Microsoft::UI::Composition;
namespace MUCSB = Microsoft::UI::Composition::SystemBackdrops;
namespace MUX = Microsoft::UI::Xaml;
namespace WS = Windows::System;
}
...
struct MainWindow : MainWindowT<MainWindow>
{
winrt::MUCSB::SystemBackdropConfiguration m_configuration{ nullptr };
winrt::MUCSB::MicaController m_backdropController{ nullptr };
winrt::MUX::Window::Activated_revoker m_activatedRevoker;
winrt::MUX::Window::Closed_revoker m_closedRevoker;
winrt::MUX::FrameworkElement::ActualThemeChanged_revoker m_themeChangedRevoker;
winrt::MUX::FrameworkElement m_rootElement{ nullptr };
winrt::WS::DispatcherQueueController m_dispatcherQueueController{ nullptr };
MainWindow::MainWindow()
{
InitializeComponent();
SetBackground();
m_closedRevoker = this->Closed(winrt::auto_revoke, [&](auto&&, auto&&)
{
if (nullptr != m_backdropController)
{
m_backdropController.Close();
m_backdropController = nullptr;
}
if (nullptr != m_dispatcherQueueController)
{
m_dispatcherQueueController.ShutdownQueueAsync();
m_dispatcherQueueController = nullptr;
}
});
}
void SetBackground()
{
if (winrt::MUCSB::MicaController::IsSupported())
{
// We ensure that there is a Windows.System.DispatcherQueue on the current thread.
// Always check if one already exists before attempting to create a new one.
if (nullptr == winrt::WS::DispatcherQueue::GetForCurrentThread() &&
nullptr == m_dispatcherQueueController)
{
m_dispatcherQueueController = CreateSystemDispatcherQueueController();
}
// Setup the SystemBackdropConfiguration object.
SetupSystemBackdropConfiguration();
// Setup Mica on the current Window.
m_backdropController = winrt::MUCSB::MicaController();
m_backdropController.SetSystemBackdropConfiguration(m_configuration);
m_backdropController.AddSystemBackdropTarget(
this->m_inner.as<winrt::MUC::ICompositionSupportsSystemBackdrop>());
}
else
{
// The backdrop material is not supported.
}
}
winrt::WS::DispatcherQueueController CreateSystemDispatcherQueueController()
{
DispatcherQueueOptions options
{
sizeof(DispatcherQueueOptions),
DQTYPE_THREAD_CURRENT,
DQTAT_COM_NONE
};
::ABI::Windows::System::IDispatcherQueueController* ptr{ nullptr };
winrt::check_hresult(CreateDispatcherQueueController(options, &ptr));
return { ptr, take_ownership_from_abi };
}
void SetupSystemBackdropConfiguration()
{
m_configuration = winrt::MUCSB::SystemBackdropConfiguration();
// Activation state.
m_activatedRevoker = this->Activated(winrt::auto_revoke,
[&](auto&&, MUX::WindowActivatedEventArgs const& args)
{
m_configuration.IsInputActive(
winrt::MUX::WindowActivationState::Deactivated != args.WindowActivationState());
});
// Initial state.
m_configuration.IsInputActive(true);
// Application theme.
m_rootElement = this->Content().try_as<winrt::MUX::FrameworkElement>();
if (nullptr != m_rootElement)
{
m_themeChangedRevoker = m_rootElement.ActualThemeChanged(winrt::auto_revoke,
[&](auto&&, auto&&)
{
m_configuration.Theme(
ConvertToSystemBackdropTheme(m_rootElement.ActualTheme()));
});
// Initial state.
m_configuration.Theme(
ConvertToSystemBackdropTheme(m_rootElement.ActualTheme()));
}
}
winrt::MUCSB::SystemBackdropTheme ConvertToSystemBackdropTheme(
winrt::MUX::ElementTheme const& theme)
{
switch (theme)
{
case winrt::MUX::ElementTheme::Dark:
return winrt::MUCSB::SystemBackdropTheme::Dark;
case winrt::MUX::ElementTheme::Light:
return winrt::MUCSB::SystemBackdropTheme::Light;
default:
return winrt::MUCSB::SystemBackdropTheme::Default;
}
}
...
};
...