WPF Child Windows problem in Windows XP

自闭症网瘾萝莉.ら 提交于 2020-01-17 04:11:26

问题


I have written a WPF program that when user clicked a button, a new window will be popped up.

I have tried to show the new window by using Show() or ShowDialog() function.

In Windows 7, when user closed the child window, the main window will remain and the program will not exit. This behavior is what I want to have.

However, when the program is run in Windows XP, when user closed the child window, the main window will be closed together and the whole program will be exited.

I have tried to set different value in different properties in Window class, finally, I found that the program will not exit only when I set the property "ShowInTaskbar" to "False" in child window.

However, if ShowInTaskbar is set to false, user cannot find the entry in task bar which is not the behavior that I want.

What I want to have is really simple. I just want the program running in Windows XP to have the same behavior as the program running in Windows 7 when user closed the child window (i.e. main window will not exit when user closed the child window). Also, I want to have an entry in task bar for a newly created child window(i.e. ShowInTaskbar = true).

Does anyone have any idea about this problem?

MainWindow

<Window x:Class="ChildWindowTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Click="OpenChild">Open Child Window</Button>
</Grid>
</Window>

Code For MainWindow:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void OpenChild(object sender, RoutedEventArgs e)
    {
        ChildWindow child = new ChildWindow();
        child.Owner = this;
        //child.ShowInTaskbar = false; <--- if comment, the program will exit, when child window closed
        child.Show();
    }
}

Child Window:

<Window x:Class="ChildWindowTest.ChildWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ChildWindow" Height="300" Width="300">
<Grid>

</Grid>

Code for Child Window:

public partial class ChildWindow : Window
{
    public ChildWindow()
    {
        InitializeComponent();
    }
}

回答1:


Not an elegant solution at all, but you always can subscribe to Closing event in Application class and cancel application closing in an event handler.




回答2:


Did u make sure you have childWindow.Owner set as our MainWindow correctly before calling childWindow.ShowDialog()?



来源:https://stackoverflow.com/questions/7459983/wpf-child-windows-problem-in-windows-xp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!