【问题标题】:How can I create a delay during a DoubleAnimation when using AutoReverse?使用 AutoReverse 时如何在 DoubleAnimation 期间创建延迟?
【发布时间】:2023-04-02 21:13:01
【问题描述】:

我有一个动画,它将网格移动到特定位置,然后使用 AutoReverse 自动返回。但由于它是即时的,用户无法阅读其中的消息。如何在动画中添加 - 例如 - 5 秒延迟

这是我目前的方法

public void ErrorMessage(Grid grid, ImageSource imageSource, String error_message)
    {
        Image_Broken_Component.Source = imageSource;
        TextBlock_Error_Message.Text = error_message;

        ThicknessAnimation ta = new ThicknessAnimation
        {
            From = grid.Margin,
            To = new Thickness(0, 50, 0, 0),
            Duration = new Duration(TimeSpan.FromSeconds(1)),
            AutoReverse = true
        };
        grid.BeginAnimation(Grid.MarginProperty, ta);
    }

动画应该像这样播放(使用 From 和 To-值):1 -> 0 -> 等待 5s -> 1

【问题讨论】:

  • 代替 AutoReverse,从第一个动画的 Completed 处理程序运行具有特定 BeginTime 的第二个动画。或者当用户点击通知确认他们已经注意到它时。

标签: c# wpf xaml animation doubleanimation


【解决方案1】:

不要使用AutoReverse 属性,而是使用Storyboard

https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/storyboards-overview

BeginTime 属性可用于延迟第二个动画以进行基本排序,如下所示:

XAML C# WPF Best efficient way to do an ordered sequence of animations

然后您可以只对两个ThicknessAnimations(一个正向,一个反向)进行排序,并设置第二个的BeginTime,以便在动画反转之前有一个暂停。

示例代码:

public void ErrorMessage(Grid grid, ImageSource imageSource, String error_message)
{
    Image_Broken_Component.Source = imageSource;
    TextBlock_Error_Message.Text = error_message;

    var storyboard = new Storyboard();

    storyboard.Children.Add(new ThicknessAnimation
    {
        From = grid.Margin,
        To = new Thickness(0, 50, 0, 0),
        Duration = new Duration(TimeSpan.FromSeconds(1))
    });

    storyboard.Children.Add(new ThicknessAnimation
    {
        From = new Thickness(0, 50, 0, 0),
        To = grid.Margin,
        Duration = new Duration(TimeSpan.FromSeconds(1)),
        BeginTime = TimeSpan.FromSeconds(6)
    });

    grid.BeginStoryboard(Grid.MarginProperty, storyboard);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    相关资源
    最近更新 更多