c# - WPF 淡出动画 - 不能再改变不透明度

标签 c# wpf

我有一个带有标签控件的 WPF 窗口,用于向用户发送消息。几秒钟后,我希望消息消失。我创建了一个 DispatcherTimer 和一个 Storyboard来执行此操作。 (计时器延迟 5 秒,然后 tick 事件触发,消息消失。)它成功消失,但下一条消息的不透明度仍设置为 0。(因此用户看不到它。)显然,我试过了将不透明度设置回 1,但无一异常(exception)地失败了。 (也就是说,我可以毫无问题地跨过那行代码,但执行后不透明度仍然为 0。) 谁能告诉我我做错了什么?下面是来自测试项目的一些代码,其中只有一个标签控件、一个用于设置标签内容和淡入淡出动画的按钮,以及一个尝试重置不透明度的按钮。

XAML:

<Window x:Class="WpfTestApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="525">
    <Grid>
        <StackPanel x:Name="stkHeaderBar" Grid.Row="0" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Orientation="Horizontal" FlowDirection="RightToLeft">
            <Label x:Name="lblTest" Content="lblTest"/>
            <Button x:Name="btnChangeLabel" Content="ChangeLabel" Click="btnChangeLabel_Click"/>
            <Button x:Name="btnResetOpacity" Content="Reset Opacity" Click="btnResetOpacity_Click"/>
        </StackPanel>
    </Grid>
</Window>

C#:

private void btnChangeLabel_Click(object sender, RoutedEventArgs e)
{
    lblTest.Content = "Testing1...Testing2...Testing3";
    lblTest.Opacity = 1;

    // Create a storyboard to contain the animations.
    Storyboard storyboard = new Storyboard();
    TimeSpan duration = new TimeSpan(0, 0, 2);

    // Create a DoubleAnimation to fade the not selected option control
    DoubleAnimation animation = new DoubleAnimation();

    animation.From = 1.0;
    animation.To = 0.0;
    animation.Duration = new Duration(duration);
    // Configure the animation to target de property Opacity
    Storyboard.SetTargetName(animation, lblTest.Name);
    Storyboard.SetTargetProperty(animation, new PropertyPath(Control.OpacityProperty));
    // Add the animation to the storyboard
    storyboard.Children.Add(animation);

    // Begin the storyboard
    storyboard.Begin(this);
}

private void btnResetOpacity_Click(object sender, RoutedEventArgs e)
{
    lblTest.Opacity = 1;
}

最佳答案

默认情况下,动画的 FillBehavior 设置为 HoldEnd,这意味着动画保持目标属性的最终值。如果您想稍后重置该值,您需要删除动画,或者将 FillBehavior 设置为 Stop。然后,您可以为动画的 Completed 事件添加一个处理程序,以手动保留最终值。

另请注意,您不需要计时器来延迟动画的开始。您可以改为设置其 BeginTime 属性。

最后,不需要 Storyboard 来为单个属性设置动画。您可以改为调用 UIElement.BeginAnimation

private void btnChangeLabel_Click(object sender, RoutedEventArgs e)
{
    var animation = new DoubleAnimation
    {
        To = 0,
        BeginTime = TimeSpan.FromSeconds(5),
        Duration = TimeSpan.FromSeconds(2),
        FillBehavior = FillBehavior.Stop
    };

    animation.Completed += (s, a) => lblTest.Opacity = 0;

    lblTest.BeginAnimation(UIElement.OpacityProperty, animation);
}

private void btnResetOpacity_Click(object sender, RoutedEventArgs e)
{
    lblTest.Opacity = 1;
}

关于c# - WPF 淡出动画 - 不能再改变不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27744097/

相关文章:

c# - 高度频繁地创建类的短期新实例是否效率低下?

c# - 参数更新后 Blazor 页面不重新呈现

wpf - 路径的圆角

wpf - 如何在 Model-View-Viewmodel 架构中从模型方法的中间干净地获取用户输入?

c# - 不返回任何内容或返回 void-- C# 在 void Function() 调用结束时究竟做了什么?

c# - 如何将此 LINQ 查询转换为延迟加载

c# - 使用 C# 在移动宽带 api windows 7 和 windows 8 上苦苦挣扎,不确定要安装什么

wpf - 在 WPF 中优雅地覆盖 ComboBox 的 ToggleButton 样式

wpf - 查找资源字典时发生错误

c# - 具有IEnumerable <Brush>作为ItemsSource和SelectedItem异常的ComboBox