You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WPF实现窗口环绕动画边框并让末端消失的问题求助

WPF环绕式动画边框实现求助

我需要在C# WPF窗口中实现环绕式动画边框,效果是边框沿窗口边缘循环移动,且末端带有透明消失的过渡效果。现在我修改了从网上找到的代码,但遇到两个问题:

  • 无法实现边框末端透明消失的效果
  • 代码逻辑混乱,且左侧边框的动画在完成约95%时就会停止

窗口每条边的边框都被分为两部分:Col1为左侧/上侧部分,Col2为右侧/下侧部分,我给不同边的动画设置了错开的开始时间,相关代码如下:

private void InitSbs()
{
     topEdgeSb = CreateSb("TopEdgeCol1", "TopEdgeCol2", "TopEdge", true, 0);
     rightEdgeSb = CreateSb("RightEdgeCol1", "RightEdgeCol2", "RightEdge", false, 500);
     bottomEdgeSb = CreateSb("BottomEdgeCol1", "BottomEdgeCol2", "BottomEdge", true, 500 * 2);
     leftEdgeSb = CreateSb("LeftEdgeCol1", "LeftEdgeCol2", "LeftEdge", false, 500 * 3);
}
private GridLengthAnimation CreateGridLengthAnim(string name, double from, double to, bool isCol,  int beginTime, int duration)
{
    GridLengthAnimation anim = new GridLengthAnimation();
    anim.From = new GridLength(from, GridUnitType.Star);
    anim.To = new GridLength(to, GridUnitType.Star);
    anim.Duration = new Duration(new System.TimeSpan(0, 0, 0, 0, duration));
    anim.BeginTime = new System.TimeSpan(0, 0, 0, 0, beginTime);
    Storyboard.SetTargetName(anim, name);
    if (isCol)
    {
        Storyboard.SetTargetProperty(anim, new PropertyPath(ColumnDefinition.WidthProperty));
    }
    else
    {
        Storyboard.SetTargetProperty(anim, new PropertyPath(RowDefinition.HeightProperty));
    }

    return anim;
}

public void Start()
{
    topEdgeSb.Begin(this, true);
    leftEdgeSb.Begin(this, true);
    bottomEdgeSb.Begin(this, true);
    rightEdgeSb.Begin(this, true);
}

我设置了edgeDuration为500ms,resetTime为500ms + beginTime,比如底部边框作为第三个动画,resetTime为500ms + 500*3ms,但目前左侧边框(第四个动画)在完成约95%时就停止了,希望能得到解决帮助。


内容的提问来源于stack exchange,提问作者user21871584

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 10:33:38