问题描述
为什么我在关闭子窗口时,连同父窗体也同时关闭了?我的代码如下:父窗体:index.xaml<UserControlx:Class="WpfOA.Views.BusinessManageView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:WpfOA.FormControl"mc:Ignorable="d"d:DesignHeight="354"d:DesignWidth="746"><Grid><TextBlockName="textBlock_AddFile"Text="添加业务文件:"/><ButtonClick="ButtonClick_ShowChildWin"Content="添加"></Button></Grid></UserControl>index.xaml.cs//打开子窗口TestChildWintestChildWin=newTestChildWin();publicvoidButtonClick_ShowChildWin(objectsender,EventArgse){testChildWin.WindowStyle=System.Windows.WindowStyle.None;testChildWin.ChangeTextEvent+=newChangeTextHandler(FuncSettingClosed);testChildWin.ShowDialog();}//设置窗口关闭时执行的方法publicvoidFuncSettingClosed(){MessageBox.Show("================");testChildWin.Close();}子窗体:TestChildWin.xaml<Windowx:Class="WpfOA.Views.TestChildWin"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="TestChildWin"Height="300"Width="300"><Grid><ButtonContent="关闭"Click="ButtonClick_Closed"Margin="5,5,0,0"Width="40"Height="30"HorizontalAlignment="Left"VerticalAlignment="Top"IsHitTestVisible="True"></Button><ButtonContent="返回"Click="Window_Closed"Margin="50,5,0,0"Width="40"Height="30"HorizontalAlignment="Left"VerticalAlignment="Top"IsHitTestVisible="True"></Button></Grid></Window>TestChildWin.xaml.csnamespaceWpfOA.Views{//定义委托publicdelegatevoidChangeTextHandler();///<summary>///TestChildWin.xaml的交互逻辑///</summary>publicpartialclassTestChildWin:Window{//定义事件publiceventChangeTextHandlerChangeTextEvent;publicTestChildWin(){InitializeComponent();}privatevoidWindow_Closed(objectsender,EventArgse){StrikeEvent();}privatevoidButtonClick_Closed(objectsender,EventArgse){this.Close();}//触发事件改变MainWindow的值privatevoidStrikeEvent(){if(ChangeTextEvent!=null){ChangeTextEvent();}}}}
解决方案
解决方案二:
一般不会出现这种情况,你是否使用了多线程?