简体   繁体   English

C# 如何处理以编程方式创建的 ColorAnimation 和 Storyboard?

[英]C# How to dispose ColorAnimation and Storyboard that are created programmatically?

im very new to storyboards and animations.我对故事板和动画很陌生。

While their setup concept is fairly straightforward, i've been struggling trying to find an efficient way to destroy them to preserve memory in my specific scenario.虽然他们的设置概念相当简单,但我一直在努力寻找一种有效的方法来摧毁它们,以在我的特定场景中保留 memory。

Basically i have a listview with several items (list view is dinamically populated by a database) when clicking a button i would like for the listview item to perform a color animation to a specific argb color, since the number of items is dinamically populated, it can have tens or hundreds of items.基本上我有一个包含多个项目的列表视图(列表视图由数据库动态填充),当单击一个按钮时,我希望列表视图项目执行颜色 animation 到特定的 argb 颜色,因为项目的数量是动态填充的,它可以有数十或数百个项目。

To make this process more efficient memory wise and cpu wise, i would like to create animations on the fly, rather than have unnecessary animations per each item, everything works fine except i feel like i need to dispose them to release unnecessary cpu and memory usage which i have no idea on how to do.为了使这个过程更有效 memory 明智和 cpu 明智,我想动态创建动画,而不是每个项目都有不必要的动画,一切正常,除了我觉得我需要处理它们以释放不必要的 cpu 和 memory 使用我不知道该怎么做。

Below you can find the code for the button click event which performs the entire operation您可以在下面找到执行整个操作的按钮单击事件的代码

employees_data_model.selected_employee_contact.DeleteEmployee = employees_data_model.selected_employee_contact.DeleteEmployee == false; employees_data_model.selected_employee_contact.DeleteEmployee = employees_data_model.selected_employee_contact.DeleteEmployee == false; var _Container = Employees_List_View.ContainerFromItem(Employees_List_View.SelectedItem); var _Container = Employees_List_View.ContainerFromItem(Employees_List_View.SelectedItem); var GridItem = FindMyChildByName(_Container, "GridItem") as Grid; var GridItem = FindMyChildByName(_Container, "GridItem") as Grid;

            if (GridItem.Background == null)
                GridItem.Background = new SolidColorBrush(ColorHelper.FromArgb(0, 0, 0, 0));

            if (GridItemColorAnimation != null)
            {
                GridItemAnimationStoryBoard.Stop();
                //Here i would like to dispose both GridItemColorAnimation and GridItemAnimationStoryBoard and create new instances below that target different item
                
            }
                GridItemColorAnimation = new ColorAnimation();
                GridItemAnimationStoryBoard = new Storyboard();

            var duration = new Duration(TimeSpan.FromMilliseconds(employees_data_model.selected_employee_contact.DeleteEmployee == true ? 300 : 1000));
            GridItemColorAnimation.Duration = duration;
            GridItemColorAnimation.EnableDependentAnimation = true;

            Storyboard.SetTarget(GridItemColorAnimation, GridItem.Background);
            Storyboard.SetTargetProperty(GridItemColorAnimation, "Color");
            Storyboard.SetTargetName(GridItemColorAnimation, "GridItem.Background");
            GridItemAnimationStoryBoard.Children.Add(GridItemColorAnimation);
            GridItemAnimationStoryBoard.Duration = duration;
            GridItemColorAnimation.To = employees_data_model.selected_employee_contact.DeleteEmployee == true ? ColorHelper.FromArgb(150, 255, 100, 100) : ColorHelper.FromArgb(0, 0, 0, 0);
            GridItemAnimationStoryBoard.Begin();

Thank you very much in advance for all your input.非常感谢您的所有意见。

How to dispose ColorAnimation and Storyboard that are created如何处理创建的 ColorAnimation 和 Storyboard

Derive from code, you will find GridItemColorAnimation was referenced by GridItemAnimationStoryBoard by calling GridItemAnimationStoryBoard.Children.Add method, so we could not release GridItemColorAnimation directly.由代码推导出GridItemColorAnimation是通过调用GridItemAnimationStoryBoard.Children.Add方法被GridItemAnimationStoryBoard引用的,所以不能直接释放GridItemColorAnimation In general, UWP will gc automatically by clr.一般来说,UWP会被clr自动gc。 If you do want to clean it manually, you could try to clear GridItemAnimationStoryBoard children collection and then set it as null then call GC.Collect() method.如果您确实想手动清理它,您可以尝试清除GridItemAnimationStoryBoard子集合,然后将其设置为 null 然后调用GC.Collect()方法。

Neither ColorAnimation or Storyboard implement IDisposable so they should not and cannot be disposed. ColorAnimationStoryboard都没有实现IDisposable ,因此它们不应该也不能被释放。

What you should do is to ensure that you are not keeping a reference to any of these objects longer than necessary.您应该做的是确保您对这些对象中的任何一个的引用不会超过必要的时间。 If there is no reference to an object, the garbage collector will eventually collect it and perhaps reclaim the memory.如果没有对 object 的引用,垃圾收集器最终将收集它并可能回收 memory。

If you create a new ColorAnimation and Storyboard and reassing your properties or variables to these each time your method is called, you should be fine assuming you don't keep references to the old objects that get overwritten from somewhere else.如果您创建一个新的ColorAnimationStoryboard并在每次调用您的方法时重新评估您的属性或变量,那么假设您不保留对从其他地方覆盖的旧对象的引用应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM