为什么在 Window 代码后台中的以下事件没有效果?
void about_Click(object sender, RoutedEventArgs e)
{
// TopLevel.Opacity = 1.0, Splashscreen.Opacity = 0.0
TopLevel.Opacity = 0.1;
// still: TopLevel.Opacity = 1.0
Splashscreen.Opacity = 1.0;
// still: Splashscreen.Opacity = 0.0
}
不透明度值不发生变化。
我发现以下触发器是问题的原因:
<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource splashscreenanimation}" />
</EventTrigger>
</Window.Triggers>
移除后端代码后,它可以正常工作。
为了完整起见,这是动画效果:
<Window.Resources>
<Storyboard x:Key="splashscreenanimation">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="Splashscreen"
BeginTime="0:0:0.900">
<EasingDoubleKeyFrame KeyTime="0:0:1.5"
Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="TopLevel"
BeginTime="0:0:0.900">
<EasingDoubleKeyFrame KeyTime="0:0:1.5"
Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
解决方法: 在代码中,您可以通过首先执行以下命令来删除动画: Splashscreen.BeginAnimation(UserControl.OpacityProperty, null);
(Splashscreen是一个用户控件)。
我还尝试向故事板添加FillBehavior="HoldEnd"
或FillBehavior="Stop"
,但无法正确地使其工作。