簡體   English   中英

UWP 項目:如何在 CODE 中將陰影 (Microsoft.Toolkit.Uwp.UI.AttachedShadowBase) 附加到 UIElement

[英]UWP Project: how to attach a drop shadow (Microsoft.Toolkit.Uwp.UI.AttachedShadowBase) to a UIElement in CODE

對,我一直在使用舊方法在代碼中執行 DropShadows ( ElementCompositionPreview ...)。

該方法被標記為已棄用。

在 XAML 中使用新的AttachedDropShadow很好,但是在 C# 代碼中呢? 我沒有看到任何關於如何在 C# 中直接使用這個助手的文檔。 我想將它附加到UIElement

有什么幫助嗎?

使用答案,以便我可以以可讀的形式發布代碼:

看看這個方法:

public static void InitShadow(FrameworkElement e, float blurRadius, float opacity, 
                              Vector3 offset, Windows.UI.Color color)
{
    if (e != null)
    {
        /*
        AttachedDropShadow shadow = new AttachedDropShadow()
        {
            Opacity = opacity,
            BlurRadius = blurRadius,
            Offset = offset.ToString(),
            Color = color
        };

        e.SetValue(Effects.ShadowProperty, null);
        e.SetValue(Effects.ShadowProperty, shadow);
        */

        // Drop any existing shadow
        ElementCompositionPreview.SetElementChildVisual(e, null);

        Compositor compositor = ElementCompositionPreview.GetElementVisual(e).Compositor;
        var shadowVisual = compositor.CreateSpriteVisual();
        shadowVisual.Size = new Vector2((float)e.ActualWidth, (float)e.ActualHeight);

        var dropShadow = compositor.CreateDropShadow();
        dropShadow.BlurRadius = blurRadius;
        dropShadow.Opacity = opacity;
        dropShadow.Offset = offset;
        dropShadow.Color = color;

        shadowVisual.Shadow = dropShadow;

        ElementCompositionPreview.SetElementChildVisual(e, shadowVisual);
    }
}

活動部分工作並產生正確的陰影。

非活動部分(評論中的部分)是我理解的提議......它不會產生陰影。

我錯過了什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM