当我在WPF中为其他窗口调用ShowDialog时,如何隐藏父窗口。 [英] How do I hide the parent window when I call ShowDialog for other window in WPF.

查看:798
本文介绍了当我在WPF中为其他窗口调用ShowDialog时,如何隐藏父窗口。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF应用程序中,一个窗口为其他窗口调用ShowDialog。我看到所有的窗户链。当我在显示其他窗口之前隐藏调用窗口时 - 当Visibility = System.Windows.Visibility.Visible时ShowDialog返回。对于所有窗口,我添加了this.Closed + = Window_Closed;显示上一个窗口

  void  Window_Closed( object  sender,EventArgs e)
{
if (Owner!= null
{
Owner.Visibility = System.Windows.Visibility.Visible;
}
}



和调用ShowDialog之前我隐藏了活动窗口

{

this.Visibility = System.Windows.Visibility.Collapsed;

CustomerData maCreateCD = new CustomerData();

maCreateCD.Owner = this;

maCreateCD.ShowDialog();

}

对其他窗口重复此操作,一切正常。我只看到最后一个窗口。但是当我在最后一个窗口中调用Close()时,我看到了maCreateCD.ShowDialog();返回,其他窗口返回ShowDialog。并且只有在此之后,将在最后一个之前调用的窗口的Visibility属性设置为Visible和窗口显示。

在MSDN中,仅在窗口关闭后返回ShowDialog,但这不起作用。我看到ShowDialog被返回,窗口没有关闭。

解决方案

更好的选择是先关闭对话框,而不是显示前一个对话框。然后显示下一个对话框。类似于:

  var  result = dialog1.ShowDialog(); 
if (some_conditions)
{
var result2 = dialog2。的ShowDialog();
}
if (some_other_conditions)
{
var result3 = dialog3.ShowDialog();
}
if (some_final_conditions)
{
var result4 = dialog4.ShowDialog();
}



我会说每个对话框都会使用主窗口作为其所有者。这样做,可以先关闭旧对话框,然后从同一个事件处理程序中显示新对话框。



我可以说模态对话框是不是为了隐藏而设计的。这种情况远非我理解不支持,因此表现不佳。一个模态对话框被设计为显示直到它结束,之后代码在ShowDialog调用之后继续在调用者位置。



如果很多事情出现问题你试图解决方法。特别是,启用,闪烁,任务栏按钮,焦点可能不正确,因此应用程序将无法与系统良好协作。 避免隐藏模态Windows。



或者,如果它是一个向导,那么你应该只为每个页面显示一个用户控件对话框。


In WPF Application one window call ShowDialog for other and other. And I see all window chain. When I hide the calling window before I show other - ShowDialog returns when Visibility = System.Windows.Visibility.Visible. For all windows I added this.Closed += Window_Closed; for show previous window

void Window_Closed(object sender, EventArgs e)
{
    if (Owner != null)
    {
            Owner.Visibility = System.Windows.Visibility.Visible;
    }
}


and before call ShowDialog I hide the active window
{
this.Visibility = System.Windows.Visibility.Collapsed;
CustomerData maCreateCD = new CustomerData();
maCreateCD.Owner = this;
maCreateCD.ShowDialog();
}
This operation repeated for other windows and all OK. I see only last window. But when I call Close() in last window I see that maCreateCD.ShowDialog(); is returned and for other windows ShowDialog is returned. And only after this the Visibility property for window called before last is set to Visible and window shows.
In MSDN ShowDialog is returned only after window closing but this does not work. I see that ShowDialog is returned and window does not closed.

解决方案

Instead of showing next dialog from the previous one, a much better alternative would be to close the dialog first and then show next dialog. Something like :

var result = dialog1.ShowDialog();
if (some_conditions)
{
  var result2 = dialog2.ShowDialog();
}
if (some_other_conditions)
{
  var result3 = dialog3.ShowDialog();
}
if (some_final_conditions)
{
  var result4 = dialog4.ShowDialog();
}


I would say that each dialog would use the main window as their owner. Thus doing it that way, it might be possible to first close the old dialog and show the new one from the same event handler.

I can say that modal dialog are not designed to be hidden. This scenario is af far as I understand not supported and thus will not behave well. A modal dialog is designed to be shown until its end and after that the code continue at the caller location just after the call of ShowDialog.

Many things ca goes wrong if you try to make a workaround. In particular, enabling, flickering, taskbar buttons, focus might not bahave correctly and thus the application won't work well with the system. Avoid hiding modal Windows.

Alternatively, if it is a wizard, then you should simply show a user control for each page Inside the same dialog.


这篇关于当我在WPF中为其他窗口调用ShowDialog时,如何隐藏父窗口。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆