-1

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.

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.

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.

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; var _Container = Employees_List_View.ContainerFromItem(Employees_List_View.SelectedItem); 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.

EDIT: Ok guys thank you very much for your help, its a shame we cannot release them in any way, what im doing is simply using the .Stop() function per each storyboard and releasing them from the list of storyboards with list.clear(), hopefully it will be enough and the GC will collect them automatically, if i find out that i still have to call the garbage collector in the end, so be it, hopefully itll be enough.

Thanks again for all of your help!

Eugen M
  • 51
  • 1
  • 6

2 Answers2

1

How to dispose ColorAnimation and Storyboard that are created

Derive from code, you will find GridItemColorAnimation was referenced by GridItemAnimationStoryBoard by calling GridItemAnimationStoryBoard.Children.Add method, so we could not release GridItemColorAnimation directly. In general, UWP will gc automatically by clr. 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.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
1

Neither ColorAnimation or Storyboard implement IDisposable so they should not and cannot be disposed.

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.

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.

mm8
  • 163,881
  • 10
  • 57
  • 88