我有一个带有StoryBoard的窗口,它存储在Window的资源中,如下所示:
<Storyboard x:Key="FadeInStoryBoard">
<DoubleAnimation Storyboard.TargetName="GridNotificationWindow" From="0.01" To="1" Storyboard.TargetProperty="Opacity" Duration="0:0:2"/>
<DoubleAnimation Storyboard.TargetName="GridNotificationWindow" From="1" To="0" Storyboard.TargetProperty="Opacity" Duration="0:0:2" BeginTime="0:0:6"/>
</Storyboard>
在代码背后如下:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Storyboard s = (Storyboard)this.TryFindResource("FadeInStoryBoard");
if(s!=null)
{
BeginStoryboard(s);
}
}
但似乎不起作用...... 如果我在xaml中设置它,它正在工作:
<EventTrigger RoutedEvent="Window.Loaded" SourceName="GridNotificationWindow">
<BeginStoryboard x:Name="FadeInStoryBoard" >
<Storyboard>
<DoubleAnimation Storyboard.TargetName="GridNotificationWindow" From="0.01" To="1" Storyboard.TargetProperty="Opacity" Duration="0:0:2"/>
<DoubleAnimation Storyboard.TargetName="GridNotificationWindow" From="1" To="0" Storyboard.TargetProperty="Opacity" Duration="0:0:2" BeginTime="0:0:6"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
如何修复代码?
答案 0 :(得分:2)
在调用 BeginStoryboard
之前设置目标 Storyboard.SetTarget(s, TargetName);