在我的窗口中,我单击一个按钮,然后在当前窗口前显示一个弹出窗口。
private void btnStandards_Click(object sender, RoutedEventArgs e)
{
StandardChooseScreen ChooseScreen = new StandardChooseScreen();
ChooseScreen.Show();
}
在弹出窗口中,用户必须勾选复选框并按“下一步”按钮。
private void btnStandardOption_Click(object sender, RoutedEventArgs e)
{
if (chkNewStandard.IsChecked == true)
{
CreateNewStandard = true;
StandardRedirect(CreateNewStandard, EditExistsingStandard, DeleteAStandard);
this.Close();
}
正如您从按钮单击中看到的那样,我正在调用StandardDirect
这是我之前的Window中的方法,但该方法需要一个实例,我不想创建一个新实例,是有办法在后台获取已经打开的窗口的实例吗?
答案 0 :(得分:4)
将其传递给弹出窗口的构造函数:
public class YourPopupWindow : Form {
private YourMainWindow _mainWindow;
public YourPopupWindow(YourMainWindow mainWindow) {
_mainWindow = mainWindow;
}
private void btnStandardOption_Click(object sender, RoutedEventArgs e) {
if (chkNewStandard.IsChecked) {
CreateNewStandard = true;
_mainWindow.StandardRedirect(CreateNewStandard, EditExistsingStandard, DeleteAStandard);
// ^^^^^^^ this
this.Close();
}
}
}
您的另一个选择是使用ShowDialog
..这样您的弹出窗口就是一个真正的模态窗口。
if (yourPopupInstance.ShowDialog() == DialogResult.OK) {
if (yourPopupInstance.chkNewStandard.IsChecked) {
CreateNewStandard = true;
StandardRedirect(CreateNewStandard, EditExistsingStandard, DeleteAStandard);
// you're still in the main window here
yourPopupInstance.Close();
}
}