ShowDialog在WPF中的处理方式与Winforms不同吗



我在将Winforms程序转换为WPF程序时遇到了另一个问题。在我的第一个程序中,我打开了一个较小的窗口,允许用户调整一些数据,然后当它关闭时,另一个表单会用新数据再次激活。

我使用form2.ShowDialog();打开表单,这会自动使Winforms中的父表单停用。这样,当我关闭form2时,父窗体被激活,并且我能够使用事件处理程序form1_Activated成功地重新加载和初始化一些设置。

但是,现在当我尝试使用WPF做同样的事情时,我仍然可以使用form2.ShowDialog();打开form2,但当我关闭表单时,它不会注册form1_Activated事件处理程序。相反,为了重新加载设置,我必须单击另一个窗口,然后返回到程序中注册form1_Activated事件处理程序。

我只是做错了什么,还是我应该在WPF中使用另一个事件处理程序来实现我在Winforms中所能做的事情?

调用ShowDialog()会导致对话框顶部以模式模式出现,所以我不明白为什么在对话框关闭后需要一个事件处理程序来处理结果。请记住,您也可以在对话框中访问公共变量。如果我理解你的问题,这应该符合你的要求:

主窗口:

My_DialogBox dlg = new My_DialogBox();
dlg.Owner = this;
dlg.MyPublicVariable = ''; //some value that you might need to pass to the dialog
dlg.ShowDialog();  //exection of MainWindow is suspended until dialog box is closed
if (dlg.DialogResult == true)
{
    //dlg.MyPublicVariable is still accessible 
    //call whatever routines you need in order to refresh the main form's data
}

对话框:

private void OK_Button_Click(object sender, RoutedEventArgs e)
{
    MyPublic variable = something;  //accessible after the dialog has closed.
    this.DialogResult = true;
}
private void Cancel_Button_Click(object sender, RoutedEventArgs e)
{
     this.DialogResult = false;
}

MSDN在对话框上的文章写得很好。有一些技巧可能会对你有更大的帮助:http://msdn.microsoft.com/en-us/library/aa969773.aspx

祝你好运!

相关内容

最新更新