我从 Win2d 库中在我的应用程序中抛出了很多异常,我试图在加载事件上对元素的背景应用模糊。
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
var visual = ElementCompositionPreview.GetElementVisual(this);
this.backgroundContainer.Size = new Vector2((float)this.backgroundGrid.ActualWidth, (float)this.backgroundGrid.ActualHeight);
// Create background color
this.backgroundColorSprite = visual.Compositor.CreateSpriteVisual();
this.backgroundColorSprite.Size = this.backgroundContainer.Size;
var darkColor = App.DynamicThemeing.BaseThirtyColor.GetValueOrDefault(Colors.Transparent);
darkColor.A = 215;
this.backgroundColorSprite.Brush = visual.Compositor.CreateColorBrush(darkColor);
this.backgroundContainer.Children.InsertAtBottom(this.backgroundColorSprite);
// Create the blur effect to be used.
var blurEffect = new GaussianBlurEffect
{
Name = "Blur",
BlurAmount = 10.0f,
BorderMode = EffectBorderMode.Hard,
Optimization = EffectOptimization.Balanced,
Source = new CompositionEffectSourceParameter("source")
};
var effectFactory = visual.Compositor.CreateEffectFactory(blurEffect, new[] { "Blur.BlurAmount" });
var effectBrush = effectFactory.CreateBrush();
effectBrush.SetSourceParameter("source", visual.Compositor.CreateBackdropBrush());
this.backgroundBlurSprite = visual.Compositor.CreateSpriteVisual();
this.backgroundBlurSprite.Brush = effectBrush;
this.backgroundBlurSprite.Size = this.backgroundContainer.Size;
this.backgroundContainer.Children.InsertAtBottom(this.backgroundBlurSprite);
}