问题描述
|
我对彩色动画有问题。这是我的来源:
<Window.Resources>
<hedit:BrushToColorConverter x:Key=\"BrushToColorConverter\" />
<Style x:Key=\"MyButtonStyle\" targettype=\"Button\">
<Setter Property=\"OverridesDefaultStyle\" Value=\"True\"/>
<Setter Property=\"Margin\" Value=\"5\"/>
<Setter Property=\"Template\">
<Setter.Value>
<ControlTemplate targettype=\"Button\">
<ControlTemplate.Resources>
<Storyboard x:Key=\"buttonAnimIn\">
<!-- Problem line -->
<ColorAnimation Storyboard.TargetName=\"bntBack\" Storyboard.TargetProperty=\"Color\" To=\"{Binding Path=Foreground,RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Converter={StaticResource BrushToColorConverter}}\" />
</Storyboard>
<Storyboard x:Key=\"buttonAnimOut\">
<ColorAnimation Storyboard.TargetName=\"bntBack\" Storyboard.TargetProperty=\"Color\" To=\"Blue\" />
</Storyboard>
<Storyboard x:Key=\"buttonAnimForegroundIn\">
<ColorAnimation Storyboard.TargetName=\"btnFore\" Storyboard.TargetProperty=\"Color\" To=\"Blue\" />
</Storyboard>
<Storyboard x:Key=\"buttonAnimForegroundOut\">
<ColorAnimation Storyboard.TargetName=\"btnFore\" Storyboard.TargetProperty=\"Color\" To=\"Red\" />
</Storyboard>
</ControlTemplate.Resources>
<Border Name=\"border\"
BorderThickness=\"1\"
Padding=\"4,2\"
BorderBrush=\"DarkGray\"
CornerRadius=\"3\">
<Border.Background>
<SolidColorBrush Color=\"Blue\" x:Name=\"bntBack\" />
</Border.Background>
<ContentControl HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Content=\"{TemplateBinding Content}\">
<ContentControl.Foreground>
<SolidColorBrush Color=\"Red\" x:Name=\"btnFore\" />
</ContentControl.Foreground>
</ContentControl >
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent=\"Button.MouseEnter\">
<BeginStoryboard Storyboard=\"{StaticResource buttonAnimIn}\" />
<BeginStoryboard Storyboard=\"{StaticResource buttonAnimForegroundIn}\" />
</EventTrigger>
<EventTrigger RoutedEvent=\"Button.MouseLeave\">
<BeginStoryboard Storyboard=\"{StaticResource buttonAnimOut}\" />
<BeginStoryboard Storyboard=\"{StaticResource buttonAnimForegroundOut}\" />
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
问题是:
无法将属性“ \ Style \”中的值转换为\\ System.Windows.Style \类型的对象。无法冻结此情节提要板时间轴树以供跨线程使用。标记文件\'HLSLEditor; component / mainwindow.xaml \'中的对象\'System.Windows.Controls.Button \'中的错误,第223行位置25。
使用固定颜色时,它可以工作,但不能与父级的前景颜色一起工作...
如何制作前景色或背景色的动画?
谢谢!
解决方法
您无法冻结Bindings,您可以通过将颜色声明为资源,然后在动画中使用StaticResource的同时将Control \的Background绑定到它来解决此问题。
例如
<Window.Background>
<SolidColorBrush Color=\"{DynamicResource Background}\"/>
</Window.Background>
<Window.Resources>
<Color x:Key=\"Background\">Green</Color>
</Window.Resources>
<ColorAnimation Storyboard.TargetProperty=\"Foreground.Color\"
Duration=\"0:0:1\"
To=\"{StaticResource Background}\"/>
使用资源类的替代方法:
public static class MyColors
{
public static Color MyHighlightColor = Color.FromArgb(255,88,0);
}
<ColorAnimation Storyboard.TargetProperty=\"Foreground.Color\"
Duration=\"0:0:1\"
To=\"{x:Static local:MyColors.MyHighlightColor}\"/>
,我认为了解错误可能会给您解决问题的方法。
动画需要使用UI线程以外的线程。因此,情节提要板必须是可冻结的,这意味着情节提要中的所有动画都必须是可冻结的,并且这些动画使用的所有内容也必须是可冻结的。
绑定是不可冻结的-顾名思义,因为绑定是一种可以更改依赖项属性的机制。您不能在彩色动画中使用动态绑定-动画运行时属性可能会更改。无论是绑定到对象还是使用DynamicResource
,都会发生相同的情况。
问题是,这可以保护您免受您根本不想要的东西的侵害。您实际上并不希望在动画运行时更改颜色。那不是您要实现的目标。如果用户选择其他外观,则希望动画使用的颜色资源进行更改。
因此,不要将情节提要板绑定到可换肤的资源,而是将情节提要板添加到在外观更改时设置的资源字典中(使用静态绑定设置颜色),并在事件触发器中使用动态绑定。那应该工作。
,当我遇到此问题时,我通过修改样式以使其彼此重叠包含两个相同的元素来解决该问题-一个用于“正常”状态,另一个用于“按下”状态。默认情况下,“按下”的ѭ6设置为0,而另一个had6设置为1。我的动画将不透明度从0更改为1,反之亦然。
这种方法实际上避免了对Color
属性进行动画处理,但是在将所有内容保留在XAML中的同时产生了相同的效果。由于颜色是在样式定义而不是动画中设置的,因此可以根据需要进行绑定。这可能并不适合所有情况,但对于我相当简单的样式而言,这是一种实现所需效果的快速方法。