在WPF动画中将属性BeginTime设置为静态资源 [英] In WPF animation set property BeginTime to a static resource
问题描述
我想做的是使用资源定义动画的所有BeginTime.
What I want to do is define all the BeginTimes of my Animation using a resource.
例如,我想要
<sys:TimeSpan x:key="SomeResource">... </sys:TimeSpan>
...
<DoubleAnimation BeginTime={StaticResource SomeResource}/>
显然sys:TimeSpan不是正确的类型.如何定义我的资源,以便在定义动画时可以将其引用为资源?
Obviously sys:TimeSpan is not the correct type to use. How do I define my resource so I can reference it as a resource when defining my animations?
我也只想在XAML中做到这一点.
I also want to do this purely in XAML.
谢谢.
推荐答案
System.TimeSpan
是正确的类型,因为这是 BeginTime
的类型.您也可以对 Duration
执行相同的操作(但改用 System.Windows.Duration
类型).
System.TimeSpan
is the correct type to use since is this is the type of BeginTime
. You can also do the same for Duration
(but using the System.Windows.Duration
type instead).
以下是在动画中使用 StaticResource
的示例(2秒后,淡入1秒):
Here is an example using a StaticResource
in an animation (after 2 seconds, fade in for 1 second):
<Button Content="Placeholder"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Opacity="0.5">
<Button.Resources>
<sys:TimeSpan x:Key="FadeInBeginTime">0:0:2</sys:TimeSpan>
<Duration x:Key="FadeInDuration">0:0:1</Duration>
</Button.Resources>
<Button.Style>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard x:Name="FadeInBeginStoryBoard">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="1"
BeginTime="{StaticResource FadeInBeginTime}"
Duration="{StaticResource FadeInDuration}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<StopStoryboard BeginStoryboardName="FadeInBeginStoryBoard" />
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
假设您已将 sys
命名空间声明为:
Assuming you have declared the sys
namespace as:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
希望这会有所帮助!
这篇关于在WPF动画中将属性BeginTime设置为静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!