WPF 实例化窗体 退出程序后 进程依然存在

晚安苏州 2017-10-24 03:21:09
举个例子:
主窗体中放一个按钮:

<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="221,139,0,0" VerticalAlignment="Top" Width="75" Click="button_Click" />
</Grid>
</Window>


private void button_Click(object sender, RoutedEventArgs e)
{
TestWindow testWindow = new TestWindow();
}


TestWindow只是一个空窗体:

<Window x:Class="WpfApplication3.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="TestWindow" Height="300" Width="300">
<Grid>

</Grid>
</Window>


点一下按钮,然后关闭程序,在任务管理器中,程序依然无法退出!如果变成;

private void button_Click(object sender, RoutedEventArgs e)
{
TestWindow testWindow = new TestWindow();
testWindow.ShowDialog();
}

就没问题,程序进程顺利退出,求大神指点迷津
...全文
2140 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
晚安苏州 2017-10-25
  • 打赏
  • 举报
回复
引用 5 楼 sudazf 的回复:
[quote=引用 4 楼 duanzi_peng 的回复:] [quote=引用 2 楼 duanzi_peng 的回复:] 点一下按钮 -》点击的什么关闭按钮?
是主窗口上的关闭按钮吧? 你在app.xaml中设置属性ShutdownMode为OnMainWindowClose。 意思就是主窗口退出,则整个Application退出。Application退出 进程就结束了,就会释放进程中的所有资源。[/quote] 原来可以这样,刚刚试了一下,是可以的![/quote] 补充一下 加上这句也是可以的:

testWindow.Owner = System.Windows.Application.Current.MainWindow;
因为: • When a child window is opened by a parent window by calling ShowDialog, an implicit relationship is established between both parent and child window. • When a child window is created by a parent window by calling Show, however, the child window does not have a relationship with the parent window. • To allow you to create a relationship between a child window and a parent window, Window supports the notion of ownership. Ownership is established when the Owner property of a window (the owned window) is set with a reference to another window (the owner window). • If you close an owner window, its owned windows are also closed.
晚安苏州 2017-10-24
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
[quote=引用 2 楼 duanzi_peng 的回复:] 点一下按钮 -》点击的什么关闭按钮?
是主窗口上的关闭按钮吧? 你在app.xaml中设置属性ShutdownMode为OnMainWindowClose。 意思就是主窗口退出,则整个Application退出。Application退出 进程就结束了,就会释放进程中的所有资源。[/quote] 原来可以这样,刚刚试了一下,是可以的!
exception92 2017-10-24
  • 打赏
  • 举报
回复
引用 2 楼 duanzi_peng 的回复:
点一下按钮 -》点击的什么关闭按钮?
是主窗口上的关闭按钮吧? 你在app.xaml中设置属性ShutdownMode为OnMainWindowClose。 意思就是主窗口退出,则整个Application退出。Application退出 进程就结束了,就会释放进程中的所有资源。
晚安苏州 2017-10-24
  • 打赏
  • 举报
回复
引用 2 楼 duanzi_peng 的回复:
点一下按钮 -》点击的什么关闭按钮?
点击的主窗体右上角的X键关闭的
exception92 2017-10-24
  • 打赏
  • 举报
回复
点一下按钮 -》点击的什么关闭按钮?
晚安苏州 2017-10-24
  • 打赏
  • 举报
回复
就是想咨询下,有没有办法,不调用ShowDialog的情况下, 释放掉TestWindow

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧