我从Win2d库中获取了很多异常,我试图将模糊应用到load事件中元素的背景。
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);
}