我有一个caliburn.micro WPF应用程序,我正在通过调用此函数从F#启动它:
>>
问题是我需要有一个对主窗口的引用,以便我可以跟踪它是否仍然打开等,但它始终为null(因此注释代码)。注意这在运行“普通”MVVM应用程序并以类似方式初始化时工作正常:
public void LaunchGUI()
{
if (_guiWindow == null)
{
var windowThread = new Thread(() =>
{
try
{
// Create our context, and install it:
SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
AppBootstrapper.Initialize();
var app = new App();
app.InitializeComponent();
//guiWindow = app.MainWindow;
//// When the window closes, shut down the dispatcher
//guiWindow.Closed += (s, e) =>
// Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
//guiWindow.Closed += (s, e) =>
// guiWindow = null;
//guiWindow.WindowState = WindowState.Minimized;
//guiWindow.Show();
//guiWindow.WindowState = WindowState.Normal;
//guiWindow.Activate();
// Start the Dispatcher Processing
Dispatcher.Run();
}
catch (Exception e)
{
LogMessage("error in gui thread: " + e);
//guiWindow.Close();
//guiWindow = null;
}
});
windowThread.SetApartmentState(ApartmentState.STA);
windowThread.IsBackground = true;
windowThread.Start();
}
else
{
_guiWindow.WindowState = WindowState.Minimized;
_guiWindow.Show();
_guiWindow.WindowState = WindowState.Normal;
_guiWindow.Activate();
}
}
}
我的问题是如何访问创建的主窗口?
答案 0 :(得分:2)
//你得到这样的应用程序主窗口:
var _guiWindow = Application.Current.MainWindow as MainWindow;