我的WPF程序没有显示GUI元素
本文关键字:显示 GUI 元素 WPF 程序 我的 | 更新日期: 2023-09-27 18:16:47
我有一个加载屏幕,从我的程序中显示,以防止我的程序在加载时看起来无响应,但是当我使用loadingScreen.Show();
和this.Hide();
时,加载屏幕窗口显示良好,但是没有来自MahApps的GUI元素。Metro出现了,标签也没有。
这是我到目前为止的代码:
LoadingScreen screen = new LoadingScreen();
screen.InitializeComponent();
this.Hide();
screen.Show();
然后是需要加载的内容,最后是
screen.Hide();
this.Show();
我认为你有线程问题。你的启动画面锁定了主线程,它永远不会到达你的主应用程序。我是这样解决这个问题的。我为启动屏幕和初始化创建了一个新线程。当初始化完成,主应用程序可以继续运行时,我使用ManualResetEvent向主线程发送信号。
public partial class App : Application
{
private static LoadingScreen splashScreen;
private static ManualResetEvent resetSplash;
[STAThread]
private static void Main(string[] args)
{
try
{
resetSplash = new ManualResetEvent(false);
var splashThread = new Thread(ShowSplash);
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.IsBackground = true;
splashThread.Name = "My Splash Screen";
splashThread.Start();
resetSplash.WaitOne(); //wait here until init is complete
//Now your initialization is complete so go ahead and show your main screen
var app = new App();
app.InitializeComponent();
app.Run();
}
catch (Exception ex)
{
//Log it or something else
throw;
}
}
private static void ShowSplash()
{
splashScreen = new LoadingScreen();
splashScreen.Show();
try
{
//this would be your async init code inside the task
Task.Run(async () => await Initialization())
.ContinueWith(t =>
{
//log it
}, TaskContinuationOptions.OnlyOnFaulted);
}
catch (AggregateException ex)
{
//log it
}
resetSplash.Set();
Dispatcher.Run();
}
}