我有一个应用程序在窗口加载时消耗大量时间。
在 Window_load 事件中,我从数据库读取一些控件的状态和名称。
我想做一个启动画面,该启动画面将在窗口完全加载后结束。
我尝试过这个例子http://www.codeproject.com/KB/dialog/wpf_animated_text_splash.aspx http://www.codeproject.com/KB/dialog/wpf_animated_text_splash.aspx但启动屏幕在主窗口完全加载之前关闭,并且我的主窗口显示为白色并且未完全加载。
我是 wpf 的初学者,我不知道如何才能让启动屏幕保留在屏幕上,直到主窗口完全加载。
请给我一个例子。
我的闪屏代码:
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);
}
}
我将在 MainWindow 构造函数中调用这个启动屏幕:
new SplashWindow().ShowDialog();
但我的 MainWindow Load 函数将在启动窗口完成显示后运行。
谢谢你!
如果您使用内置SplashScreen http://msdn.microsoft.com/en-us/library/system.windows.splashscreen.aspx上课,可以打电话Show(false) http://msdn.microsoft.com/en-us/library/cc681209.aspx指定您将负责关闭启动屏幕。然后您可以使用Close() http://msdn.microsoft.com/en-us/library/system.windows.splashscreen.close.aspx方法来关闭它。
请注意,SplashScreen
类仅支持显示静态图像。不过,这样做有很好的理由——尽快将启动屏幕显示在用户面前。
代码看起来像这样:
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));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)