winui-3winuiwindows-community-toolkitwin2d

Use of GaussianBlurEffect in Winui 3 and CommunityToolkit.WINUUI.UI version 8


How can use GaussianBlurEffect in WINUI 3 and CommunityToolkit.WINUI,UI versione 8?

public static CompositionEffectBrush CreateBlurEffectBrush(double amount = 0.0)
{
    var effect = new GaussianBlurEffect
    {
        Name = "Blur",
        BlurAmount = (float)amount,
        Source = new CompositionEffectSourceParameter("source")
    };

    var compositor = App.MainWindow.Compositor;
    var factory = compositor.CreateEffectFactory(effect as IGraphicsEffect, new[] { "Blur.BlurAmount" }); // Cast to IGraphicsEffect
    var brush = factory.CreateBrush();
    brush.SetSourceParameter("source", compositor.CreateBackdropBrush());
    return brush;
}

Solution

  • You need to install the Microsoft.Graphics.Win2D NuGet package.

    Then make the MainWindow accessible.

    App.xaml.cs

    using Microsoft.UI.Xaml;
    
    namespace WinUIApp;
    
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    
        public static Window MainWindow { get; } = new MainWindow();
    
        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            MainWindow.Activate();
        }
    }