如何正确停止以编程方式声明的故事板

时间:2016-10-18 13:33:42

标签: wpf wpf-animation

我有一个WPF应用程序,当数据源中的值更新并且工作正常时,它会执行单元格闪烁。

我注意到应用程序的分析,当我快速执行更新时,内存正在增长。因为我以这种方式启动故事板

 private static DoubleAnimation blink =
        new DoubleAnimation()
        {
            From = 0,
            To = 1,
            Duration = TimeSpan.FromSeconds(0.2),
            FillBehavior = FillBehavior.Stop
        };
 private void Items_ItemChanged(object sender, ItemChangedEventArgs<ExpandoObject> e)
    {
       var club = e.Item;
       var row = grid.ItemContainerGenerator.ContainerFromItem(club) as Telerik.Windows.Controls.GridView.GridViewRow;
       if (row != null)
       {
          var column = grid.Columns[e.PropertyName];
          var cell = row.Cells.FirstOrDefault(c => c.Column == column);
          if (cell != null)   //In case of virtualization I won't have all the columns
          {
              Storyboard.SetTarget(blink, cell.Content as TextBlock);
              Storyboard.SetTargetProperty(blink, new PropertyPath(Button.OpacityProperty));
              var sb = new Storyboard();
              sb.Children.Add(blink);
              sb.Begin();
          }
       }
    }

我怀疑我要停止动画,但我不知道我该怎么做?

由于

2 个答案:

答案 0 :(得分:1)

Storyboard取出Items_ItemChanged

    Storyboard sb = new Storyboard();
    private void Items_ItemChanged(object sender, ItemChangedEventArgs<ExpandoObject> e)
    {
        sb.Stop();
        sb.Children.Clear();

        sb.Children.Add(blink);

        var club = e.Item;
        var row = grid.ItemContainerGenerator.ContainerFromItem(club) as Telerik.Windows.Controls.GridView.GridViewRow;
        if (row != null)
        {
            var column = grid.Columns[e.PropertyName];
            var cell = row.Cells.FirstOrDefault(c => c.Column == column);
            if (cell != null)   //In case of virtualization I won't have all the columns
            {
                Storyboard.SetTarget(blink, cell.Content as TextBlock);
                Storyboard.SetTargetProperty(blink, new PropertyPath(Button.OpacityProperty));

                sb.Begin();
            }
        }
    }

答案 1 :(得分:0)

问题在于,每次项目更改时都要创建新的故事板而不是重用现有的故事板:

var sb = new Storyboard();

如果你真的必须这样做,那么你需要处理故事板完成事件并将其从单元格中删除。