在界面 MainWindow 里弹出一个新窗体 Window1:
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="215,137,0,0" Click="button_Click"
VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
private void button_Click(object sender, RoutedEventArgs e)
{
Window1 w1 = new Window1();
w1.ShowDialog();
}
1.首先,新窗体Window1里啥控件都不放,默认的如下:
<Window x:Class="WpfApplication1.Window1"
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:WpfApplication1"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<Grid>
</Grid>
</Window>
弹出界面并关闭,操作数次之后,内存里是没有Window1这个实例的:

2.其次,我在Window1里加一个默认的DataGrid:
<Window x:Class="WpfApplication1.Window1"
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:WpfApplication1"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<Grid>
<DataGrid Width="200" Height="200" Background="Brown">
</DataGrid>
</Grid>
</Window>
弹出Window1界面并关闭,操作数次,最终关闭Window1界面,内存里始终是有Window1这个实例的:

真的是服了,总是遇到这些奇葩情况,为什么呢?是不是这种弹出窗体做法不对?我手动释放 ,调用GC回收也不行:
private void button_Click(object sender, RoutedEventArgs e)
{
Window1 w1 = new Window1();
w1.ShowDialog();
w1 = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
请高人指点
