在WPF中打开一个窗口并关闭所有其他窗口 [英] Open a window and close all other in WPF

查看:102
本文介绍了在WPF中打开一个窗口并关闭所有其他窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我被困在我的WPF应用程序中。我有一个用户控件,我在其上放置了许多不同的链接(按钮),可以打开其他表单。现在点击这些链接,我想要打开一个表单,然后关闭所有表单。我使用的代码如下,我知道这永远不会让我想要的方式发生:



Hi,
I am stuck in my WPF application. I have a user control, on which I have placed many different link(buttons) which would open other forms. Now on the click of those links I want a single form to open and let all be closed. The code I am using is as below, and I know that this would never let this happen the way I want :

private void btnOEP_Click(object sender, RoutedEventArgs e)
        {
           ExistingProject objEP = new ExistingProject();
             CloseAllWindows();
             objEP.ShowDialog(); 
        }

        private void btnCNP_Click(object sender, RoutedEventArgs e)
        {
            AddNewProjects objAddNew = new AddNewProjects();
            CloseAllWindows();
            objAddNew.ShowDialog();
           
        }

private void CloseAllWindows()
       {
           for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
               App.Current.Windows[intCounter].Close();
       }







我面临的问题是我正在打开表格对话,之后我将关闭应用程序的所有窗体窗口(使用此代码),这反过来将关闭所需的窗口。我需要关闭所有表格,除了我给出的命令保持开放。怎么可能这样做。请提出解决方案。



谢谢




The problem which I am facing is that I am opening a form dialogue, and after that I am closing all the form windows of the application(using this code), which in turn will will close the required window from also. I need to close all the forms except the one I have given the command to stay open. How could this be done. Please suggest a solution.

Thanks

推荐答案

我刚用你的代码设置了一个测试项目。



有几个mod它对我有用。



首先,你使用 ShowDialog (),这意味着您无法再先访问主窗体以启动另一个窗口,而无需先手动关闭上一个窗口。



所以请注意,这对我有用:



I have just set up a test project using your code.

With a couple of mods it works for me.

Firstly, you are using ShowDialog(), this means that you can no longer access the main form to launch another window without first closing the previous one manually.

So bearing that in mind, this works for me:

private void btnOEP_Click(object sender, RoutedEventArgs e)
{
  CloseAllWindows();
  ExistingProject objEP = new ExistingProject();
  objEP.Show();
}

private void btnCNP_Click(object sender, RoutedEventArgs e)
{
  CloseAllWindows();
  AddNewProjects objAddNew = new AddNewProjects();
  objAddNew.Show();
}

private void CloseAllWindows()
{
  for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
      App.Current.Windows[intCounter].Close();
}


OK。



这是我的想法问题。



App.Current.Windows 集合包含 all 应用程序的窗口,包括其主窗口,应该是 App.Current.Windows [0]



因此,如果关闭集合中的每个窗口,则关闭主窗口。当主窗口关闭时,它关闭应用程序。



所以我如果你修改你的代码:



OK.

Here's what I think is the problem.

The App.Current.Windows collection contains all the windows for your application, including its main window which should be App.Current.Windows[0].

Therefore if you close every window in the collection you are closing the main window. When the main window closes it closes the application.

So I think that if you modify your code to either:

private void CloseAllWindows()
{
    for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
        App.Current.Windows[intCounter].Close();
}





或:



or:

private void CloseAllWindows()
{
    for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 1; intCounter--)
        App.Current.Windows[intCounter].Close();
}





那么它应该可以工作。



希望这会有所帮助。 :)



then it should work.

Hope this helps. :)


我无法交叉检查这个,因为目前我在家里,我的机器上没有安装VS.

I can not cross check this because at present I am at home and I don't have VS installed on my machine.
NewWindow objNew = new NewWindow(); //Create an object of the window you want to open
CloseAllWindows(); //close all other windows
objNew.ShowDialog(); //Now use the ShowDialog command, and I guess it will work fine as the object reference has been created in the memory.





如果不正确,请提供反馈。





Anurag



Provide your feedback if it doesn't goes right.


Anurag


这篇关于在WPF中打开一个窗口并关闭所有其他窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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