问题描述
我正在尝试在包含在 ControlTemplate 中的 Storyboard 中使用 DynamicResource.
I am trying to use a DynamicResource in Storyboard contained within a ControlTemplate.
但是,当我尝试执行此操作时,出现无法冻结此 Storyboard 时间轴树以供跨线程使用"错误.
But, when I try to do this, I get a 'Cannot freeze this Storyboard timeline tree for use across threads' error.
这里发生了什么?
推荐答案
不,您不能在包含在 Style 或 ControlTemplate 中的 Storyboard 中使用 DynamicResource.事实上,你也不能使用数据绑定表达式.
No, you can't use a DynamicResource in a Storyboard that is contained within a Style or ControlTemplate. In fact, you can't use a data binding expression either.
这里的故事是,Style 或 ControlTemplate 中的所有内容都必须能够安全地跨线程使用,而计时系统实际上会尝试冻结 Style 或 ControlTemplate 以使其成为线程安全的.但是,如果存在 DynamicResource 或数据绑定表达式,则无法冻结它们.
The story here is that everything within a Style or ControlTemplate must be safe for use across threads and the timing system actually tries to freeze the Style or ControlTemplate to make them thread-safe. However, if a DynamicResource or data binding expression is present, it is unable to freeze them.
有关详细信息,请参阅:MSDN 链接.查看样式中的动画"和控件模板中的动画"部分(此文档页面很长).
For more info see: MSDN Link. Check out the 'Animate in a Style' and the 'Animate in a ControlTemplate' sections (this documentation page is rather long).
有关解决方法(至少对于我的情况),请参阅:WPF 论坛帖子.
And for a workaround (at least for my scenario) see: WPF Forum Post.
希望这对某人有所帮助.我掉的头发已经够多了.
Hope this helps someone. I've lost more than enough hair on it.
科里
这篇关于您可以在包含在 Style 或 ControlTemplate 中的 Storyboard 中使用 DynamicResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!