如何在给定 FrameworkElement 的情况下检索 CompositionEffect?

How can I retrieve a CompositionEffect given a FrameworkElement?

我正在尝试链接效果并将其设置在图像控件 (TestImage) 上,如下所示:

    private void TestImage_ImageOpened(object sender, RoutedEventArgs e)
    {
        /*
            (input) Backdrop -> GaussianBlur -> |
                                 ColorSource -> | Blend -> Saturation (output)
         */

        var compositor = ElementCompositionPreview.GetElementVisual(this.TestImage).Compositor;

        var blurEffect = new GaussianBlurEffect()
        {
            Name = "Blur",
            BlurAmount = 10f,
            BorderMode = EffectBorderMode.Hard,
            Optimization = EffectOptimization.Balanced,
            Source = new CompositionEffectSourceParameter("source"),
        };

        var colorEffect = new ColorSourceEffect
        {
            Name = "Tint",
        };

        var blendEffect = new BlendEffect
        {
            Background = blurEffect,
            Foreground = colorEffect,
            Mode = BlendEffectMode.Overlay,
        };

        var saturationEffect = new SaturationEffect
        {
            Name = "Saturation",
            Source = blendEffect,
            Saturation = 1.5f,
        };

        var factory = compositor.CreateEffectFactory(saturationEffect, new[]
        {
            "Blur.BlurAmount",
            "Tint.Color",
            "Saturation.Saturation",
        });

        var brush = factory.CreateBrush();
        brush.SetSourceParameter("source", compositor.CreateBackdropBrush());

        // Animatable properties
        brush.Properties.InsertScalar("Blur.BlurAmount", 10f);
        brush.Properties.InsertColor("Tint.Color", Color.FromArgb(128, 255, 0, 0));
        brush.Properties.InsertScalar("Saturation.Saturation", 1.5f);

        var sprite = compositor.CreateSpriteVisual();
        sprite.Brush = brush;
        sprite.Size = new Vector2((float)this.TestImage.ActualWidth, (float)this.TestImage.ActualHeight);
        ElementCompositionPreview.SetElementChildVisual(this.TestImage, sprite);
    }

我的目标是稍后检索该效果,以便我可以读取该效果的一些值。如何仅使用 TestImage 控件和 Composition 层获得效果?

我使用附加的 属性 解决了我的问题,因为本机不支持此功能:

internal static SpriteVisual GetSprite(DependencyObject obj) => (SpriteVisual)obj.GetValue(SpriteProperty);

internal static void SetSprite(DependencyObject obj, SpriteVisual value) => obj.SetValue(SpriteProperty, value);

/// <summary>
/// Composition Sprite attached to a FrameworkElement.
/// </summary>
internal static readonly DependencyProperty SpriteProperty =
    DependencyProperty.RegisterAttached(
        "Sprite",
        typeof(SpriteVisual),
        typeof(MyAnimations),
        new PropertyMetadata(null));