动画“风格” WPF 中使用 ObjectAnimationUsingKeyFrames 的 Control 属性
我正在尝试使用 ObjectAnimationUsingKeyFrames 为“Style”属性设置动画。当我运行下面的示例时,我只看到空窗口,没有任何异常。
几乎相同的示例可以在 Silverlight 中运行。在 WPF 中,如果我直接分配控件的“Style”属性,它也可以工作。有谁知道是否可以在 WPF 中为“Style”属性设置动画?
非常感谢。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources>
<ResourceDictionary>
<Style x:Key="TestStyle" TargetType="Control">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Canvas x:Name="Rectangle">
<Rectangle Width="200" Height="150" Fill="Red"/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Canvas>
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Target" Storyboard.TargetProperty="Style" >
<DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="{StaticResource ResourceKey=TestStyle}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
<Canvas.Children>
<ContentControl x:Name="Target"/>
</Canvas.Children>
</Canvas>
I am trying to animate 'Style' property using ObjectAnimationUsingKeyFrames. When I run the sample below, I just see empty window and there are no any exceptions.
Almost the same sample works in Silverlight. In WPF it works too, if I assign 'Style' property of the control directly. Does anyone know if it is possible to animate 'Style' property in WPF?
Many thanks.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources>
<ResourceDictionary>
<Style x:Key="TestStyle" TargetType="Control">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Canvas x:Name="Rectangle">
<Rectangle Width="200" Height="150" Fill="Red"/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Canvas>
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Target" Storyboard.TargetProperty="Style" >
<DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="{StaticResource ResourceKey=TestStyle}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
<Canvas.Children>
<ContentControl x:Name="Target"/>
</Canvas.Children>
</Canvas>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
ObjectAnimationUsingKeyFrames
尝试将动画设置为从DependencyObject
派生的值时,它会尝试首先冻结对象。如果对象无法冻结,则会抛出异常并且动画不会运行。如果您要对您编写的自定义类型的值进行动画处理,则您似乎需要从
Freezable
派生,或者不从DependencyObject
派生。对于派生自
DependencyObject
而不是Freezable
的已存在属性,您无法为它们设置动画(StyleProperty
或TemplateProperty
> 就是恰当的例子)。尝试在样式内部使用属性设置器:将所有转换逻辑构建到样式中,而不是在不同样式之间切换。您可能遇到的挑战是目标属性必须是依赖项属性,因此您不能使用
IsLoaded
。我希望你觉得这很有用。
最后一个想法:可以定义自定义动画,尽管我有我自己没做过。您有可能编写自己的自定义“ObjectAnimation”,而不受
Freezable
或非DependencyObject
类的限制。When
ObjectAnimationUsingKeyFrames
tries to animate to a value that is derived fromDependencyObject
, it attempts to freeze the object first. If the object can't be frozen, it throws an exception and the animation does not run.If you are animating a value of a custom type that you wrote, it appears you need to either derive from
Freezable
or NOT derive fromDependencyObject
.For properties that already exist that derive from
DependencyObject
and notFreezable
, you can't animate them (StyleProperty
orTemplateProperty
are cases in point). Try using a property setter inside of a style:Build all of the transition logic into the style instead of switching between different styles. A challenge that you may have with this is that the target property has to be a dependency property so you can't use
IsLoaded
.I hope you find this useful.
One final thought: It is possible to define custom animations, although I have not done this myself. There's an outside chance that you could write your own custom "ObjectAnimation" that would not be restricted to
Freezable
or non-DependencyObject
classes.