将合成效果放在控件后面

时间:2016-11-01 22:10:45

标签: c# visual-studio uwp

我在一个简单的TextBlock控件上应用了阴影效果,但是我遇到了一个问题:阴影位于Textblock的前面,我不知道如何将阴影放在TextBlock后面。你有这个问题的解决方案吗? 有一些代码可以创建DropShadow效果:

public void SetupSimpleTextShadow(TextBlock shadowTarget)
    {
        Visual hostVisual = ElementCompositionPreview.GetElementVisual(shadowTarget);
        Compositor compositor = hostVisual.Compositor;

        DropShadow dropShadow = compositor.CreateDropShadow();
        dropShadow.Color = Color.FromArgb(255, 50, 50, 50);
        dropShadow.BlurRadius = 7f;
        dropShadow.Offset = new Vector3(5f, 5f, 0f);
        dropShadow.Opacity = 0.9f;
        dropShadow.Mask = shadowTarget.GetAlphaMask(); 

        SpriteVisual shadowVisual = compositor.CreateSpriteVisual();
        shadowVisual.Shadow = dropShadow;

        ElementCompositionPreview.SetElementChildVisual(shadowTarget, shadowVisual);

        ExpressionAnimation bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size");
        bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual);

        shadowVisual.StartAnimation("Size", bindSizeAnimation);
    }

1 个答案:

答案 0 :(得分:0)

尝试使用XPShadow。示例链接为https://github.com/brookshi/XPShadow

在Nuget中找到XPShadow并下载它。

编写ref NameSpace:xmlns:xp="using:XP"

在您希望控件具有阴影的代码中,您可以在xp:Shadow中使用该控件。

              <xp:Shadow CornerRadius="2"
                           IsCached="True"
                           Z_Depth="2">
                         <Control/>
              </xp:Shadow>
相关问题