WPF 初始屏幕和主窗口加载事件
本文关键字:窗口 加载 事件 屏幕 WPF | 更新日期: 2023-09-27 18:35:17
基本上,我的应用程序在主窗口的事件LOADED
做了很多事情。
我只想在这段时间(LOADED
事件)显示一些启动画面,根本不显示主窗口。
我在这里找到了一些例子 http://www.codeproject.com/Articles/38291/Implement-Splash-Screen-with-WPF 但它不是一个好例子。这个提示也没有涵盖我的目的。
我还想在初始屏幕下方的 LOADED 事件下显示我在每个阶段所做的事情。所以我想在初始屏幕上添加状态更新。每个已完成的任务都将更新初始屏幕状态标签。
任何线索如何做到?
谢谢!
这是适合您的小代码。
void TemplateSelector_Loaded(object sender, RoutedEventArgs e)
{
showWin();
Thread.Sleep(10000);
CloseWin();
}
private void CloseWin()
{
WindowManager.Close();
}
Window tempWindow = new Window();
public void showWin()
{
WindowManager.LaunchWindowNewThread<SplashWindow>();
}
}
public class WindowManager
{
private static Window win;
public static void LaunchWindowNewThread<T>() where T : Window, new()
{
Thread newWindowThread = new Thread(ThreadStartingPoint);
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
Func<Window> f = delegate
{
T win = new T();
return win;
};
newWindowThread.Start(f);
}
private static void ThreadStartingPoint(object t)
{
Func<Window> f = (Func<Window>)t;
win = f();
win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
win.Topmost = true;
win.Show();
Dispatcher.Run();
}
public static void Close()
{
if (win != null)
win.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Send);
}
}
您可以根据需要进行修改。
我在这里找到了一些很酷的解决方案 http://plaincolumn.blogspot.com/2009/11/wpf-splash-screen-with-status-update.html
所以它 100% 符合我的需求。