【发布时间】:2022-01-09 22:24:24
【问题描述】:
我对故事板和动画非常陌生。
虽然它们的设置概念相当简单,但我一直在努力寻找一种有效的方法来销毁它们以在我的特定场景中保留内存。
基本上我有一个包含多个项目的列表视图(列表视图由数据库动态填充)当单击一个按钮时,我希望列表视图项目执行特定 argb 颜色的颜色动画,因为项目的数量是动态的填充,它可以有数十或数百个项目。
为了使这个过程在内存和 cpu 方面更有效,我想动态创建动画,而不是每个项目都有不必要的动画,一切正常,除了我觉得我需要处理它们以释放不必要的 cpu和内存使用情况,我不知道该怎么做。
您可以在下面找到执行整个操作的按钮单击事件的代码
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();
非常感谢您的所有意见。
编辑:好的,非常感谢你们的帮助,很遗憾我们不能以任何方式发布它们,我所做的只是使用每个故事板的 .Stop() 函数并将它们从故事板列表中释放出来list.clear(),希望够了,GC会自动收集它们,如果我发现我最后还是要调用垃圾收集器,就这样吧,希望够了。
再次感谢您的所有帮助!
【问题讨论】:
标签: c# uwp visual-studio-2022 winui-3