WPF DataGrid 无法被释放?

晚安苏州 晚安苏州 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
遇到奇怪的现象,只要在界面里加入DataGrid控件,界面内存实例就无法释放,举个例子:

在界面 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();
}


请高人指点

...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
晚安苏州
WPF/Silverlight 8,757 成员 12,031 内容