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



我有一个WPF应用程序,当数据源中的值被更新时执行单元格闪烁,它工作得很好。

我注意到分析应用程序时,当我执行更新非常快,内存正在增长。因为我是这样开始storyboard的

 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();
          }
       }
    }

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

谢谢

Items_ItemChanged中取出Storyboard

    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();
            }
        }
    }

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

var sb = new Storyboard();

如果你真的必须这样做,那么你需要处理storyboard finished事件并将其从单元格中移除。

相关内容

  • 没有找到相关文章

最新更新