如何在加载MainWindow时为其自动设置动画

时间:2013-04-06 19:15:43

标签: c# wpf animation mainwindow

我对wpf相当新。我想在主屏幕上显示淡入淡出动画时立即显示在屏幕上。这可能吗?请帮帮我

1 个答案:

答案 0 :(得分:6)

您可以在窗口StoryBoard事件上应用loaded,以便为窗口显示淡入淡出效果。这应该适合你 -

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="(Window.Opacity)"
                                 From="0.0" To="1.0" Duration="0:0:1"
                                 AutoReverse="False"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

如果您不想在XaML中执行此操作,也可以在窗口loaded事件处理程序中的代码中执行此操作 -

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DoubleAnimation animation = new DoubleAnimation(0, 1, 
                                    (Duration)TimeSpan.FromSeconds(1));
        this.BeginAnimation(UIElement.OpacityProperty, animation);
    }