【发布时间】:2013-12-14 15:21:07
【问题描述】:
为什么窗口代码隐藏中的以下事件无效?
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>
Solution:在后面的代码中,您可以通过首先执行来删除动画:Splashscreen.BeginAnimation(UserControl.OpacityProperty, null);
(启动画面是一个用户控件)。
我还尝试将FillBehavior="HoldEnd" 或FillBehavior="Stop" 添加到情节提要中,但没有让它正常工作。
【问题讨论】:
-
我不同意你的最后评论......这对我来说很好。也许您可以展示一个仍然表现出这种行为的 XAML 的简化版本?