首页 > 解决方案 > C# How to change width of border rapidly?

问题描述

I want to make slide in animation. I want to do it by increasing the border width. To visualize, I have a border that is behind an ellipse and to make it slide in I want to rapidly increase its width like shown in code.

while(Border.Width < 150)
{
      Border.Width += 4;
      System.Threading.Thread.Sleep(70);
}

The problem is that it wont increase width of border until the whole process is finished no matter if I stop the thread for 70ms or 1000ms.

标签: c#wpf

解决方案


您的代码中的问题是您正在使用该 thread.sleep 阻塞 UI 线程,因此在您停止阻塞之前它无法执行任何操作。

您可以使您的父方法异步并使用:

await.Task.Delay(70);

而不是睡觉。

但是你真的应该研究动画。

边框宽度的WPF动画


推荐阅读