如果您想要可重用Storyboards
的动画,您可以在中定义etc。Window.Resources
这些被称为Resources
不Classes
例子:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="233" Width="405" Name="UI" >
<Window.Resources>
<Storyboard x:Key="MyAnimation" Storyboard.TargetProperty="Opacity">
<DoubleAnimation From="0" To="1" Duration="0:0:5" />
</Storyboard>
</Window.Resources>
<Grid>
<Button Content="Animate" Name="button1" Opacity="0" >
<Button.Style>
<Style TargetType="{x:Type Button}" >
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MyAnimation}" />
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
要从后面的代码访问这些资源,您可以使用FindResource
namespace WpfApplication8
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var storyboard = (Storyboard)FindResource("MyAnimation");
}
}
}