1

我对动画比较陌生,所以我正在寻找一些方向。假设我List<Label>加载了一个带有 15 个标签的标签。我有DoubleAnimation在 500 毫秒内将不透明度从 0 设置为 1 的动画。我想循环,这些标签每 1000 毫秒开始一次动画。所以label1从0ms开始,label2从1000ms开始等等。

我知道我可以将 a 设置Storyboard.Duration为 1000ms*15 并将我的标签添加到 SB 并开始它,但是动画播放的速度与添加到 SB 的速度一样快。有没有办法以特定的时间间隔将动画添加到 SB?

编辑: 我不再使用List<Label>了这是我写的代码

class MessageWriter
{
    Storyboard _sb = new Storyboard();
    public MessageWriter()
    {

    }

    public void ProcessMessage(string _Message, WrapPanel _Panel)
    {
        string[] _Words = _Message.Split(new char[1]{Convert.ToChar(" ")});
        int _Counter = 1;
        _sb = new Storyboard();
        foreach (string _Word in _Words)
        {
            _Panel.Children.Add(ProcessWord(_Word));
            if (_Counter < _Words.Length)
            {
                _Panel.Children.Add(ProcessWord(" "));
            }
            _Counter++;
        }
        _sb.Begin();
    }

    private StackPanel ProcessWord(string _Word)
    {
        Debug.Print(_Word);
        StackPanel _Panel = new StackPanel();
        _Panel.Orientation = Orientation.Horizontal;
        _Panel.Margin = new System.Windows.Thickness(0, 0, 0, 0);

        foreach (char _c in _Word.ToList())
        {
            Label _Label = new Label();
            _Label.Opacity = 0;
            _Label.Margin = new System.Windows.Thickness(0, 0, 0, 0);
            _Label.Padding = new System.Windows.Thickness(0, 0, 0, 0);
            _Label.Content = _c.ToString();
            _Panel.Children.Add(_Label);
            AnimateLabel(_Label);
        }
        return _Panel;
    }

    private void AnimateLabel(Label _Label)
    {
        DoubleAnimation _da = new DoubleAnimation();
        _da.From = 0;
        _da.To = 1;

        int _MillSec = 500;
        _da.Duration = new System.Windows.Duration(new TimeSpan(0, 0, 0, 0, _MillSec));

        Storyboard.SetTargetProperty(_da, new PropertyPath(FrameworkElement.OpacityProperty));
        Storyboard.SetTarget(_da, _Label);
        _sb.Children.Add(_da);
    }
}
4

1 回答 1

3

您可以简单地设置每个动画的BeginTime

而且绝对不需要故事板。只需将动画应用于标签:

DoubleAnimation opacityAnimation = new DoubleAnimation
{
    To = 1d,
    Duration = TimeSpan.FromSeconds(0.5),
    BeginTime = TimeSpan.FromSeconds((double)labelIndex)
};
labelIndex++;
label.BeginAnimation(UIElement.OpacityProperty, opacityAnimation);

为什么每个变量名上都有奇怪的下划线?有广泛接受的.Net 命名约定

于 2012-04-06T22:19:53.703 回答