是一个新手,我正在使用框架3.5来玩Windows Presentation Foundation,我刚开始使用故事板和动画。我尝试使用Story Board +动画实例创建Canvas实例和动画画布,这很有趣。现在我想提出一些有条理的方法,我可以轻松地定义许多动画并使用它们(在代码中定义对象实例太繁琐)。
我听说过使用XAML来做这件事,我在CodeProject.com上看过一些例子,但他们似乎都在用它们里面的故事板定义UserControls。这对我来说似乎太难了,我想做的只是在故事板标签中定义故事板标签和动画标签,作为一个类,是否可以这样做?我的意思是,是否可以在XAML中定义一个类(不是用户控件),然后每当我想使用该特定动画时调用其变量如“StoryBoard1”或“StoryBoard2”?我该如何实施呢?
答案 0 :(得分:0)
如果您想要可重复使用的动画,可以在Storyboards
中定义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");
}
}
}