有没有办法为WPF应用程序显示启动画面?

时间:2010-10-12 11:38:53

标签: wpf splash-screen

Dupe:WPF animated splash screen

我想为我的WPF应用程序显示一个启动画面。 我想要做的是在从文件加载字典时显示它(加载大约需要5-6秒)。有没有办法在WPF中实现这一目标?我会很感激一些教程,因为这比我发布的其他问题要复杂一点。

3 个答案:

答案 0 :(得分:18)

SplashScreen实际上只是另一个没有边框的窗口,它不可调整大小(也不能以任何方式与它交互)。您可能希望将其隐藏在任务栏中,将其置于屏幕中央等。使用各种设置进行游戏,直到获得所需效果。

这是一个快速的,我在大约5分钟内掀起来证明这个理论:

<Window x:Class="MyWhateverApp.MySplashScreen"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ShowInTaskbar="False" 
        ResizeMode="NoResize" 
        WindowStartupLocation="CenterScreen"
        WindowStyle="None" 
        Background="Transparent" 
        AllowsTransparency="True"
        Title="Sandbox Splash Screen" 
        SizeToContent="Width" 
        Topmost="True" 
        Height="{Binding RelativeSource={RelativeSource Self}, 
                         Path=ActualWidth}">

    <Border CornerRadius="8" Margin="15">
        <Border.Background>
            <ImageBrush ImageSource="Resources\sandtexture.jpeg" 
                        Stretch="Fill" />
        </Border.Background>
        <Border.Effect>
            <DropShadowEffect Color="#894F3B" 
                              BlurRadius="10" 
                              Opacity="0.75" 
                              ShadowDepth="15" />
        </Border.Effect>

        <TextBlock FontSize="40"
                   FontFamily="Bauhaus 93"
                   Foreground="White"
                   Margin="10"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Text="WPF 3.5 Sandbox">
            <TextBlock.Effect>
                <DropShadowEffect Color="Black" />
            </TextBlock.Effect>
        </TextBlock>        
    </Border>
</Window>

接下来,修改App.xaml文件以删除启动窗口,而不是引发启动事件:

<Application x:Class="MyWhateverApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
    <Application.Resources>

    </Application.Resources>
</Application>

在代码隐藏中,以您认为最好的方式处理Application_Startup事件。例如:

Window1 mainWindow = null;

private void Application_Startup(object sender, StartupEventArgs e)
{
    MySplashScreen splash = new MySplashScreen();
    splash.Show();
    mainWindow = new Window1();
    mainWindow.Show();
    splash.Close();
}

答案 1 :(得分:12)

请参阅WPF 3.5 SP1: Splash Screen

或者在VS2010内点击Solution Explorer执行Add -> New Item,从已安装模板列表中选择WPFSplash Screen应位于列表中间的底部

注意:在构造函数之后以及主窗口Window_Loaded回调之前/之后,将删除初始屏幕。我把我的所有初始化都移到了主窗口构造函数中,它很有效,而且非常简单。

答案 2 :(得分:7)

简短回答:添加 - &gt;新商品 - &gt;启动画面。它在项目中转储PNG - 只需修改它。请注意,它支持完整的alpha透明度,因此可以包含阴影等...

相关问题