C# 在for循环中设置wpf控件的动画

C# 在for循环中设置wpf控件的动画,c#,wpf,storyboard,C#,Wpf,Storyboard,我正在尝试实现一个动画,其中我使用for循环将控件从一行移动到另一行 private void AnimateStoryBoard(int number) { Storyboard _currentStoryBoard = new Storyboard(); //Row Animation Int32Animation newrowanimation = new Int32Animation(); newrowani

我正在尝试实现一个动画,其中我使用for循环将控件从一行移动到另一行

    private void AnimateStoryBoard(int number)
    {
        Storyboard _currentStoryBoard = new Storyboard();

       //Row Animation
        Int32Animation newrowanimation = new Int32Animation();
        newrowanimation.From = number;
        newrowanimation.To = number+1;
        newrowanimation.Duration = new TimeSpan(0, 0, 0, 0, 500);

        Storyboard.SetTargetProperty(newrowanimation, new PropertyPath("(Grid.Row)"));

        _currentStoryBoard.Children.Add(newrowanimation);

        Storyboard.SetTarget(newrowanimation, myrectangle);

        _currentStoryBoard.Begin();
    }
我称之为使用

        for (int i = 0; i < 10; i++)
        {
            AnimateStoryBoard(i);
        }
for(int i=0;i<10;i++)
{
动画黑板(i);
}
现在当我运行这个时,我希望动画从1到2,然后从2到3,然后从3到4…9-10。但是,动画直接跳到9,然后跳到10


还有,在XAML中如何实现这一点?请注意,这里的数字10只是一个示例。数字必须来自代码隐藏,并且会不断变化。

正如Alexander Clare在评论中提到的,您必须在故事板中设置多个动画。使用循环的解决方案不起作用,因为您的方法在运行循环时不会返回,因此UI线程将无法呈现故事板/动画引起的更改

一种解决方案是单个
故事板
实例,其中包含可变数量的动画(每行一个动画)。使用
BeginTime
属性交错动画。我建议在这些动画之间使用40ms到100ms的值(我不会低于20ms)

在代码中,这看起来像这样:

private void AnimateStoryboard(int number)
{
    // Create the storyboard that will be played
    var storyboard = new Storyboard();

    // Create the animation objects for each row change and add them to the storyboard
    var currentBeginTime = TimeSpan.Zero;
    for (var i = 0; i < number; i++)
    {
        var animation = new Int32Animation();
        // Set all the properties that you set on your animation objects before, and additionally use BeginTime
        animation.BeginTime = currentBeginTime;
        storyboard.Children.Add(animation);

        // Update the currentBeginTime to achieve the staggering effect
        currentBeginTime += TimeSpan.FromMilliseconds(50);
    }

    // Finally, start the Storyboard to run all animations
    storyboard.Begin();

}
private void AnimateStoryboard(整数)
{
//创建将要播放的情节提要
var storyboard=新故事板();
//为每行更改创建动画对象,并将其添加到情节提要中
var currentBeginTime=时间跨度为零;
对于(变量i=0;i
正如Alexander Clare在评论中提到的,您必须在故事板中设置多个动画。使用循环的解决方案不起作用,因为您的方法在运行循环时不会返回,因此UI线程将无法呈现故事板/动画引起的更改

一种解决方案是单个
故事板
实例,其中包含可变数量的动画(每行一个动画)。使用
BeginTime
属性交错动画。我建议在这些动画之间使用40ms到100ms的值(我不会低于20ms)

在代码中,这看起来像这样:

private void AnimateStoryboard(int number)
{
    // Create the storyboard that will be played
    var storyboard = new Storyboard();

    // Create the animation objects for each row change and add them to the storyboard
    var currentBeginTime = TimeSpan.Zero;
    for (var i = 0; i < number; i++)
    {
        var animation = new Int32Animation();
        // Set all the properties that you set on your animation objects before, and additionally use BeginTime
        animation.BeginTime = currentBeginTime;
        storyboard.Children.Add(animation);

        // Update the currentBeginTime to achieve the staggering effect
        currentBeginTime += TimeSpan.FromMilliseconds(50);
    }

    // Finally, start the Storyboard to run all animations
    storyboard.Begin();

}
private void AnimateStoryboard(整数)
{
//创建将要播放的情节提要
var storyboard=新故事板();
//为每行更改创建动画对象,并将其添加到情节提要中
var currentBeginTime=时间跨度为零;
对于(变量i=0;i
我认为没有必要重新发明轮子:也就是为了这个目的

因此,要创建所需的动画,可以使用以下方法:

Storyboard storyBoard = new Storyboard();
int gridRowLimit = 5; // here you can set how many rows your grid has

Int32AnimationUsingKeyFrames intKeyFrame = new Int32AnimationUsingKeyFrames();
intKeyFrame.Duration = new TimeSpan(0, 0, 0, 0, gridRowLimit * 500);

for (int i = 1; i < gridRowLimit; i++)
{
    intKeyFrame.KeyFrames.Add(new DiscreteInt32KeyFrame(i));
}

Storyboard.SetTargetProperty(intKeyFrame, new PropertyPath("(Grid.Row)"));
Storyboard.SetTarget(intKeyFrame, yourControl);

storyBoard.Children.Add(intKeyFrame);
storyBoard.Begin();
情节提要情节提要=新情节提要();
int gridRowLimit=5;//您可以在此处设置网格的行数
Int32AnimationUsingKeyFrames intKeyFrame=新建Int32AnimationUsingKeyFrames();
intKeyFrame.Duration=新的时间跨度(0,0,0,0,gridRowLimit*500);
对于(int i=1;i

我希望这会有帮助。

我认为没有必要重新发明轮子:它们也用于此目的

因此,要创建所需的动画,可以使用以下方法:

Storyboard storyBoard = new Storyboard();
int gridRowLimit = 5; // here you can set how many rows your grid has

Int32AnimationUsingKeyFrames intKeyFrame = new Int32AnimationUsingKeyFrames();
intKeyFrame.Duration = new TimeSpan(0, 0, 0, 0, gridRowLimit * 500);

for (int i = 1; i < gridRowLimit; i++)
{
    intKeyFrame.KeyFrames.Add(new DiscreteInt32KeyFrame(i));
}

Storyboard.SetTargetProperty(intKeyFrame, new PropertyPath("(Grid.Row)"));
Storyboard.SetTarget(intKeyFrame, yourControl);

storyBoard.Children.Add(intKeyFrame);
storyBoard.Begin();
情节提要情节提要=新情节提要();
int gridRowLimit=5;//您可以在此处设置网格的行数
Int32AnimationUsingKeyFrames intKeyFrame=新建Int32AnimationUsingKeyFrames();
intKeyFrame.Duration=新的时间跨度(0,0,0,0,gridRowLimit*500);
对于(int i=1;i

我希望这会有所帮助。

不要等到上一个动画完成后再开始下一个动画。这将保留最后一个动画。您应该等待动画在循环中完成。这似乎与您之前提出的问题完全相同:。不要删除和重新发布问题。修复您最初发布的问题。不要等到上一个动画完成后再开始下一个动画。这将保留最后一个动画。您应该等待动画在循环中完成。这似乎与您之前提出的问题完全相同:。不要删除和重新发布问题。修复你最初发布的问题。正是我想要的..谢谢正是我想要的..谢谢