我尝试按顺序加载两个窗口,但第一个无法正确加载。可能是什么原因?我是否用第二个窗口数据阻塞了一些 UI 渲染线程?
private void loadFormWithLoadingScreen<Type>()
where Type : Window, new()
{
//first step:
LoadingScreen loadingScreen = new LoadingScreen
{
Owner = this,
Topmost = true
};
this.Hide();
loadingScreen.Activate();
loadingScreen.Show();
loadingScreen.toCenter(this);
//second step:
Type win = new Type
{
Owner = this,
Topmost = true
};
win.Unloaded += (_s, _e) =>
{
this.Show();
this.Activate();
};
win.Loaded += (_s, _e) =>
{
loadingScreen.Close();
};
win.toCenter(this);
win.Show();
}
您的标签表示它在 WPF 中,您可以在 OnStartup 方法中执行所有这些操作。这就是我一直在 WPF 中制作加载屏幕和启动屏幕的方式。
不要忘记在 App.xaml 中更改您的 StartupUri。
protected override void OnStartup(StartupEventArgs e)
{
var splashScreen = new CoolScreen();
//any other application work here. CoolScreen class can handle the rest.
splashScreen.WindowStartupLocation = WindowStartupLocation.CenterScreen;
splashScreen.Show();
var yourMainWindow = new MainWindow();
yourMainWindow.ContentRendered += (sender, args) =>
{
//This doesn't have to be in the ContentRendered method
// but it is necesary because you'll be closing out a window before another is open.
Application.Current.MainWindow = yourMainWindow;
Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
//Accessing the dispatcher of the thread for splashScreen from another
// thread could be dangerous but, you do need to have the other thread close the splashScreen window out.
splashScreen.Dispatcher.Invoke(() => splashScreen.Close());
};
yourMainWindow.WindowStartupLocation = WindowStartupLocation.Manual;
yourMainWindow.Show();
}
我可以根据需要对其进行编辑。我做了很多假设。