繁体   English   中英

您可以在样式或控件模板中包含的 Storyboard 中使用动态资源吗

[英]Can You Use A DynamicResource in a Storyboard Contained Within Style Or ControlTemplate

我正在尝试使用包含在 ControlTemplate 中的 Storyboard 中的 DynamicResource。

但是,当我尝试这样做时,出现“无法冻结此 Storyboard 时间线树以供跨线程使用”错误。

这里发生了什么?

不可以,您不能在Style或ControlTemplate内的情节提要中使用DynamicResource。 实际上,您也不能使用数据绑定表达式。

这里的故事是,Style或ControlTemplate中的所有内容都必须在跨线程中使用是安全的,并且计时系统实际上试图冻结Style或ControlTemplate以使其成为线程安全的。 但是,如果存在DynamicResource或数据绑定表达式,则无法冻结它们。

有关更多信息,请参见: MSDN Link 查看“以样式制作动画”和“以ControlTemplate制作动画”部分(此文档页面相当长)。

有关变通办法(至少在我的情况下),请参见: WPF论坛帖子

希望这对某人有帮助。 我已经失去了足够多的头发。

科里

在某些情况下,有一种解决方法:

  • 引入附加属性,
  • 使用所需的 setter 为引入的属性指定样式触发器

虽然您可以在ControlTemplate中拥有DynamicResource ,但您不能在StoryBoard中拥有一个。

我使用Opacity (或Visibility )hack 解决了这个问题。 您可以向ControlTemplate添加两个元素。 他们每个人都使用一个DynamicResources但只有一个是可见的。 您可以通过Storyboard设置每个元素的VisibilityOpacity

简单的解决方案是在代码中使用 sb

    public static void ColorAnimation(FrameworkElement Obj,  string From, string To, int Milliseconds)
    {
        Color from = ( Color )ColorConverter.ConvertFromString( From );
        Color to = ( Color )ColorConverter.ConvertFromString( To );
        {
            ColorAnimation animation = new ColorAnimation();
            animation.From = from;
            animation.To = to;
            animation.Duration = new Duration( TimeSpan.FromMilliseconds( Milliseconds ) );
            Storyboard.SetTargetProperty( animation, new PropertyPath( "(Grid.Background).(SolidColorBrush.Color)", null ) );
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add( animation );
            storyboard.Begin( Obj );
        }
    }

在 mouse_leave 事件中使用:(或也在 mouse_enter 中)

if( 你的主题决定因素 1 ) ColorAnimation( MinimizeButton, "#FF333333", "#00202020", 150 ); if( 你的主题决定因素 2 ) ColorAnimation( MinimizeButton, "#FF32506E", "#0032506E", 200 );

ETC...

暂无
暂无

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

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