有没有办法为 WPF 应用程序显示启动画面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3914213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to show a splash screen for WPF application?
提问by sokolovic
Dupe: WPF animated splash screen
Dupe:WPF 动画启动画面
I would like to show a splash screen for my WPF application. what I want to do is to show it while I load dictionary from a file (it takes about 5-6 seconds to load). Is there a way to achieve this in WPF? I would appreciate some tutorial, since this is a little bit more complicated then other questions I posted.
我想为我的 WPF 应用程序显示启动画面。我想要做的是在从文件加载字典时显示它(加载大约需要 5-6 秒)。有没有办法在 WPF 中实现这一目标?我会很感激一些教程,因为这比我发布的其他问题要复杂一些。
采纳答案by Richard Harrison
See WPF 3.5 SP1: Splash Screen
Or within VS2010 click on the Solution Explorer
do Add -> New Item
, select WPF
from the list of installed templates and Splash Screen
should be on the bottom of the list in the middle.
或者在 VS2010 中点击Solution Explorer
do Add -> New Item
,WPF
从已安装的模板列表中选择,Splash Screen
应该在列表的底部中间。
Note: The splash screen is removed after the constructor and before/when the main window Window_Loaded
callback. I moved all of my initialisation into the main window constructor and it works a treat, and is very easy.
注意:初始屏幕在构造函数之后和主窗口Window_Loaded
回调之前/时被删除。我将我所有的初始化都移到了主窗口构造函数中,这很好用,而且非常简单。
回答by Wonko the Sane
A SplashScreen is really just another Window with no border, and it is not resizable (nor can you interact with it in any way). You'd probably want to hide it from the task bar, center it on the screen, etc. Play around with various settings until you get the effect that you want.
SplashScreen 实际上只是另一个没有边框的 Window,并且它不能调整大小(也不能以任何方式与之交互)。您可能希望将其从任务栏中隐藏,将其置于屏幕中央等。尝试各种设置,直到获得您想要的效果。
Here's a quick one I whipped up in about 5 minutes to prove the theory:
这是我在大约 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>
Next, modify your App.xaml file to remove the startup window, and instead raise the Startup event:
接下来,修改您的 App.xaml 文件以删除启动窗口,而是引发 Startup 事件:
<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>
And in the code-behind, handle the Application_Startup event in whatever way you think is best. For example:
在代码隐藏中,以您认为最好的方式处理 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();
}
回答by Basic
Short answer: Add -> New Item -> Splash Screen. It dumps a PNG in the project - just modify this. note that it supports full alpha transparencey so can contain drop-shadows, etc...
简短回答:添加 -> 新项目 -> 启动画面。它在项目中转储一个 PNG - 只需修改它。请注意,它支持完全 alpha 透明,因此可以包含阴影等...