Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF中同一对象上同时出现的两个动画_C#_Wpf_Animation_Code Behind - Fatal编程技术网

C# WPF中同一对象上同时出现的两个动画

C# WPF中同一对象上同时出现的两个动画,c#,wpf,animation,code-behind,C#,Wpf,Animation,Code Behind,我有一个自定义用户控件。它的一部分是一个表示选择的矩形。我想在用户绘制后设置动画。我有以下方法 private AnimationTimeline StartRectangleHeightAnimation(Rectangle rectangle) { return new DoubleAnimation( SUB_SELECTION_HEIGHT_RATIO * selectionCanvas.RenderSize.Height, TimeSpan.F

我有一个自定义用户控件。它的一部分是一个表示选择的矩形。我想在用户绘制后设置动画。我有以下方法

private AnimationTimeline StartRectangleHeightAnimation(Rectangle rectangle)
{
    return new DoubleAnimation(
        SUB_SELECTION_HEIGHT_RATIO * selectionCanvas.RenderSize.Height, 
        TimeSpan.FromMilliseconds(500));
}
对于矩形高度和

private AnimationTimeline StartRectangleMarginAnimation(Rectangle rectangle)
{
    Thickness t = new Thickness(rectangle.Margin.Left,
        (selectionCanvas.RenderSize.Height - rectangle.Height) / 2,
        rectangle.Margin.Right, rectangle.Margin.Bottom);

    ThicknessAnimation animation = new ThicknessAnimation(t, TimeSpan.FromMilliseconds(500));
    animation.EasingFunction = new ExponentialEase()
    {
        EasingMode = EasingMode.EaseOut,
        Exponent = -5
    };
    animation.Completed += (s, e) =>
    {
        // DO STUFF.
    };
    return animation;
}
现在,这些动画中的每一个都独立工作,我可以做

var animation = StartRectangleMarginAnimation(rectangle);
rectangle.BeginAnimation(Rectangle.MarginProperty, animation);

但不是两者都有。据我所知,一个动画“覆盖”了另一个。所以我们需要一个故事板,所以。我现在有

AnimationTimeline shrink = StartRectangleHeightAnimation(rectangle);
AnimationTimeline move = StartRectangleMarginAnimation(rectangle);

Storyboard.SetTarget(shrink, rectangle);
Storyboard.SetTargetProperty(shrink, new PropertyPath(Rectangle.HeightProperty));

Storyboard.SetTarget(move, rectangle);
Storyboard.SetTargetProperty(move, new PropertyPath(Rectangle.MarginProperty));

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(shrink);
storyboard.Children.Add(move);
storyboard.Begin(this);

但这会设置高度动画,但不会设置边距。我做错了什么?

我在窗口中创建了一个矩形

<Rectangle x:Name="rectangle" Fill="#FF4A4AE8" HorizontalAlignment="Left" Height="100" Margin="44,69,100,50" Stroke="Black" VerticalAlignment="Top" Width="100"/>

每件事都能同时正常地进行。稍后我将删除此答案。

您的代码工作正常,请确保StartRectangleMarginAnimation中的厚度有效(没有负值)。请注意,您的放松功能并不平滑(指数-5),因此将两个动画的速度降低到5秒(从0.5开始),您将在接近结束时看到它是如何发挥作用的(因为指数放松具有较大的负指数)
<Rectangle x:Name="rectangle" Fill="#FF4A4AE8" HorizontalAlignment="Left" Height="100" Margin="44,69,100,50" Stroke="Black" VerticalAlignment="Top" Width="100"/>
  private AnimationTimeline StartRectangleMarginAnimation(Rectangle rectangle)
    {           
        Thickness t = new Thickness(rectangle.Margin.Left+20, rectangle.Margin.Top-50, rectangle.Margin.Right, rectangle.Margin.Bottom);

        ThicknessAnimation animation = new ThicknessAnimation(t, TimeSpan.FromMilliseconds(500));
        animation.EasingFunction = new ExponentialEase()
        {
            EasingMode = EasingMode.EaseOut,
            Exponent = -5
        };
        animation.Completed += (s, e) =>
        {
            // DO STUFF.
        };
        return animation;
    }
    private AnimationTimeline StartRectangleHeightAnimation(Rectangle rectangle)
    {
        return new DoubleAnimation(
            20,
            TimeSpan.FromMilliseconds(500));
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        AnimationTimeline shrink = StartRectangleHeightAnimation(rectangle);
        AnimationTimeline move = StartRectangleMarginAnimation(rectangle);

        Storyboard.SetTarget(shrink, rectangle);
        Storyboard.SetTargetProperty(shrink, new PropertyPath(Rectangle.HeightProperty));

        Storyboard.SetTarget(move, rectangle);
        Storyboard.SetTargetProperty(move, new PropertyPath(Rectangle.MarginProperty));

        Storyboard storyboard = new Storyboard();
        storyboard.Children.Add(shrink);
        storyboard.Children.Add(move);
        storyboard.Begin(this);
    }