WPF 启动画面直到 Windows 结束加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6121892/
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
WPF splash screen until windows ends loading
提问by Animor
I have an application that consumes a lot of time when the window loading. In the Window_load event, I read from the database the state and the name of some controls. I want to do a splash screen that will ends after the window will fully load.
我有一个应用程序在窗口加载时会消耗大量时间。在 Window_load 事件中,我从数据库中读取了一些控件的状态和名称。我想做一个在窗口完全加载后结束的启动画面。
I have tried with this example http://www.codeproject.com/KB/dialog/wpf_animated_text_splash.aspxbut the splash screen closes before the main window is fully loaded and my mainwindow appears in white and is not fully loaded.
我已经尝试过这个例子http://www.codeproject.com/KB/dialog/wpf_animated_text_splash.aspx但启动画面在主窗口完全加载之前关闭,我的主窗口显示为白色并且没有完全加载。
I am beginner in wpf, and I don't know how can I have a splash screen which remain on the screen until the main window fully loads.
我是 wpf 的初学者,我不知道如何在主窗口完全加载之前保留在屏幕上的启动画面。
Please give me an example.
请给我一个例子。
My Splash Screen Code:
我的启动画面代码:
public partial class SplashWindow : Window
{
Thread loadingThread;
Storyboard Showboard;
Storyboard Hideboard;
private delegate void ShowDelegate(string txt);
private delegate void HideDelegate();
ShowDelegate showDelegate;
HideDelegate hideDelegate;
public SplashWindow()
{
InitializeComponent();
showDelegate = new ShowDelegate(this.showText);
hideDelegate = new HideDelegate(this.hideText);
Showboard = this.Resources["showStoryBoard"] as Storyboard;
Hideboard = this.Resources["HideStoryBoard"] as Storyboard;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
loadingThread = new Thread(load);
loadingThread.Start();
}
private void load()
{
Thread.Sleep(6000);
this.Dispatcher.Invoke(showDelegate, "first data to loading");
Thread.Sleep(6000);
//load data
this.Dispatcher.Invoke(hideDelegate);
Thread.Sleep(6000);
this.Dispatcher.Invoke(showDelegate, "second data loading");
Thread.Sleep(6000);
//load data
this.Dispatcher.Invoke(hideDelegate);
Thread.Sleep(6000);
this.Dispatcher.Invoke(showDelegate, "last data loading");
Thread.Sleep(6000);
//load data
this.Dispatcher.Invoke(hideDelegate);
//close the window
Thread.Sleep(6000);
this.Dispatcher.Invoke(DispatcherPriority.Normal,(Action)delegate() { Close(); });
}
private void showText(string txt)
{
txtLoading.Text = txt;
BeginStoryboard(Showboard);
}
private void hideText()
{
BeginStoryboard(Hideboard);
}
}
And this splash screen I will call in my MainWindow constructor:
我将在 MainWindow 构造函数中调用这个启动画面:
new SplashWindow().ShowDialog();
But my MainWindow Load function will run after the Splash Window will finish to be showed.
但是我的 MainWindow Load 函数将在 Splash Window 完成显示后运行。
Thank you!
谢谢!
回答by Kent Boogaart
If you use the built-in SplashScreen
class, you can call Show(false)
to specify that you will be responsible for closing the splash screen. You can then use the Close()
method to close it.
如果您使用内置SplashScreen
类,您可以调用Show(false)
以指定您将负责关闭启动画面。然后您可以使用该Close()
方法将其关闭。
Note that the SplashScreen
class only supports displaying a static image. It does this for very good reasons though - to get the splash screen up in front of your user as soon as possible.
请注意,SplashScreen
该类仅支持显示静态图像。这样做是有充分理由的 - 尽快将启动画面呈现在您的用户面前。
Code would look something like this:
代码看起来像这样:
static class Entry
{
static void Main(string[] args)
{
var splashScreen = new SplashScreen("path/to/your/image.png");
splashScreen.Show(false);
InitializeLogging();
InitializeServices();
InitializeUserInterface();
InitializeWhateverElseYouNeed();
splashScreen.Close(TimeSpan.FromSeconds(1));
}
}
回答by miroxlav
Dynamic splash screen (with progress updates etc.)
动态启动画面(带有进度更新等)
For the best results, you can use two-phase approach to SplashScreen:
为了获得最佳结果,您可以对 SplashScreen 使用两阶段方法:
Phase 1. Display static splash screen even before .NET code starts loading.
阶段 1. 甚至在 .NET 代码开始加载之前显示静态启动画面。
Static image loads using native code yet before .NET is initialized. Very effective to inform the user as soon as possible. There is a special wayto achieve this.
在 .NET 初始化之前使用本机代码加载静态图像。尽快通知用户非常有效。有一种特殊的方法可以实现这一点。
Phase 2. Display custom form once .NET is loaded.
阶段 2. 加载 .NET 后显示自定义表单。
Design your custom form looking identicallyto your static splash screen when initially displayed. Showing the form will automatically fade-out static splash screen shown in Phase 1 (it is done by WPF) and since then you are free to display loading progress of your application. Hide this form (which can be always-on-topwindow) when your main window finishes loading its data.
设计您的自定义表单,其外观与最初显示时的静态启动画面相同。显示表单将自动淡出阶段 1 中显示的静态闪屏(由 WPF 完成),从那时起您可以自由显示应用程序的加载进度。当您的主窗口完成加载其数据时隐藏此表单(它可以是始终在顶部的窗口)。
回答by PaulB
If your splash screen is just an image - add the image to the project and set it's Build Action property to 'SplashScreen'. The framework will handle the rest. (VS2008 sp1 or later).
如果您的初始屏幕只是一个图像 - 将图像添加到项目并将其构建操作属性设置为“SplashScreen”。框架将处理其余的。(VS2008 sp1 或更高版本)。
If you need the splash screen to differ (maybe displaying a version number) this approach wont work. If you want this take a look at the SplashScreenclass that gives a little more flexibility.
如果您需要不同的初始屏幕(可能显示版本号),则此方法将不起作用。如果你想要这个,看看SplashScreen类,它提供了更多的灵活性。
回答by biju
May be because your data loading is complete but your UI thread have not finished rendering yet.Make sure that your UI is completely rendered before closing the splash.
可能是因为你的数据加载完成了,但是你的UI线程还没有完成渲染。在关闭splash之前确保你的UI已经完全渲染好了。
Have a look the below links.
看看下面的链接。
http://www.codeproject.com/KB/WPF/WPFsplashscreen.aspx
http://www.codeproject.com/KB/WPF/WPFsplashscreen.aspx
http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/
http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/