我正在应用 AcrylicBrush 所以它必须直接分配给控件。
您可以使用compositor
将亚克力应用于SpriteVisual
. 在我们有Acrylic API之前,我们使用Win2D制作GaussianBlurEffect
。获取UIElement,并将其用于. 然后设置为喜欢以下。UIElement
Compositor
Compositor
CreateSpriteVisual
GaussianBlurEffect
hostSprite.Brush
Compositor _compositor;
SpriteVisual _hostSprite;
private void applyAcrylicAccent(Panel panel)
{
_compositor = ElementCompositionPreview.GetElementVisual(panel).Compositor;
_hostSprite = _compositor.CreateSpriteVisual();
_hostSprite.Size = new Vector2((float)panel.ActualWidth, (float)panel.ActualHeight);
var backdrop = _compositor.CreateHostBackdropBrush();
// Use a Win2D blur affect applied to a CompositionBackdropBrush.
var graphicsEffect = new GaussianBlurEffect
{
Name = "Blur",
BlurAmount = 100f,
Source = new CompositionEffectSourceParameter("backdrop")
};
var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" });
var effectBrush = effectFactory.CreateBrush();
effectBrush.SetSourceParameter("backdrop", backdrop);
_hostSprite.Brush = effectBrush;
ElementCompositionPreview.SetElementChildVisual(panel, _hostSprite);
}
并用 applyAcrylicAccent(RootLayout) 调用它。您还需要处理SizeChanged
事件:
private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (_hostSprite != null)
_hostSprite.Size = e.NewSize.ToVector2();
}