动画“风格” WPF 中使用 ObjectAnimationUsingKeyFrames 的 Control 属性

发布于 2024-10-15 21:18:14 字数 1866 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-10-22 21:18:14

ObjectAnimationUsingKeyFrames尝试将动画设置为从DependencyObject派生的值时,它会尝试首先冻结对象。如果对象无法冻结,则会抛出异常并且动画不会运行。

如果您要对您编写的自定义类型的值进行动画处理,则您似乎需要从 Freezable 派生,或者不从 DependencyObject 派生。

对于派生自 DependencyObject 而不是 Freezable 的已存在属性,您无法为它们设置动画(StylePropertyTemplateProperty > 就是恰当的例子)。尝试在样式内部使用属性设置器:

<Style.Triggers>
  <Trigger Property="IsEnabled" Value="True">
    <Setter Property="Template" Value="{StaticResource TestTemplate}"/>
  </Trigger>
</Style.Triggers>

将所有转换逻辑构建到样式中,而不是在不同样式之间切换。您可能遇到的挑战是目标属性必须是依赖项属性,因此您不能使用 IsLoaded

我希望你觉得这很有用。

最后一个想法:可以定义自定义动画,尽管我有我自己没做过。您有可能编写自己的自定义“ObjectAnimation”,而不受 Freezable 或非 DependencyObject 类的限制。

When ObjectAnimationUsingKeyFrames tries to animate to a value that is derived from DependencyObject, 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 from DependencyObject.

For properties that already exist that derive from DependencyObject and not Freezable, you can't animate them (StyleProperty or TemplateProperty are cases in point). Try using a property setter inside of a style:

<Style.Triggers>
  <Trigger Property="IsEnabled" Value="True">
    <Setter Property="Template" Value="{StaticResource TestTemplate}"/>
  </Trigger>
</Style.Triggers>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文