我的 WPF 程序不显示 GUI 元素
My WPF program is not displaying GUI elements
我有一个从我的程序中显示的加载屏幕,以防止我的程序在加载时看起来没有响应,但是当我使用 loadingScreen.Show();
和 this.Hide();
时,加载屏幕 window 显示正常,但是来自 MahApps.Metro 的 GUI 元素中的 none 出现,标签也没有出现。
这是我目前的代码:
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();
}
}
我有一个从我的程序中显示的加载屏幕,以防止我的程序在加载时看起来没有响应,但是当我使用 loadingScreen.Show();
和 this.Hide();
时,加载屏幕 window 显示正常,但是来自 MahApps.Metro 的 GUI 元素中的 none 出现,标签也没有出现。
这是我目前的代码:
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();
}
}