我弹出一个窗口,如果主窗口不存在,则会创建一个主窗口:
if (App.Current.MainWindow != null && App.Current.MainWindow.GetType() == typeof(MainWindow))
{
this.Close();
}
else
{
//main Window hasn't been created yet so create it!
MainWindow main = new MainWindow();
App.Current.MainWindow = main;
this.Close();
main.Show();
wndw = main;
}
如何引用 MainWindow 以便可以在 if 语句之外使用它?
我希望能够执行 main.txbox.Text = ....
等操作
这是我尝试过的:
MainWindow main;
if
{...
}
else
{...
}
main= App.Current.MainWindow;
或MainWindow main = App.Current.MainWindow();
这两种方法似乎都不起作用。我确信这真的很简单,我只是无法弄清楚语法。帮忙?
编辑:类似于 this question我之前问过,但有所不同,因为在这里我问的是引用当前打开的窗口时的语法。类似的问题是指如何检测哪个窗口被打开。它们很相似,但我不想偏离主题。
最佳答案
试试这个方法
var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
关于c# - 如何在WPF中引用当前打开的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31544090/