我正在使用基础设施 TabGroupPane。在这种情况下,当 ContentPane 处于活动状态时,我将加载用户控制并删除以前活动的选项卡内容。这是我的代码 Xaml :
<igDock:DocumentContentHost>
<igDock:SplitPane>
<igDock:TabGroupPane x:Name="tabMainMenu" SelectionChanged="xamMainTab_SelectionChanged" AllowDrop="False">
<igDock:ContentPane x:Name="tabItem1" Header="Item 1"></igDock:ContentPane>
<igDock:ContentPane x:Name="tabItem2" Header="Item 2"></igDock:ContentPane>
<igDock:ContentPane x:Name="tabItem3" Header="Item 3"></igDock:ContentPane>
<igDock:ContentPane x:Name="tabItem4" Header="Item 4"></igDock:ContentPane>
<igDock:ContentPane x:Name="tabItem5" Header="Item 5"></igDock:ContentPane>
</igDock:TabGroupPane>
</igDock:SplitPane>
</igDock:DocumentContentHost>
和代码背后
private void xamMainTab_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0 && null != e.RemovedItems[0] as ContentPane)
{
(e.RemovedItems[0] as ContentPane).Content = null;
}
GC.Collect();
ContentPane selectedTab = this.tabMainMenu.SelectedItem as ContentPane;
BindContainer(selectedTab.Name);
}
private void BindContainer(string item)
{
switch (item)
{
case "tabItem1":
this.tabItem1.Content = new UserControls.UserControl1();
this.tabMainMenu.SelectedItem = this.tabItem1;
break;
case "tabItem2":
this.tabItem2.Content = new UserControls.UserControl2();
this.tabMainMenu.SelectedItem = this.tabItem2;
break;
case "tabItem3":
this.tabItem3.Content = new UserControls.UserControl3();
this.tabMainMenu.SelectedItem = this.tabItem3;
break;
case "tabItem4":
this.tabItem4.Content = new UserControls.UserControl4();
this.tabMainMenu.SelectedItem = this.tabItem4;
break;
case "tabItem5":
this.tabItem5.Content = new UserControls.UserControl5();
this.tabMainMenu.SelectedItem = this.tabItem5;
break;
default:
this.tabItem1.Content = new UserControls.UserControl1();
this.tabMainMenu.SelectedItem = this.tabItem1;
break;
}
}
在这里,我将最后一项的内容设置为 null。(e.RemovedItems[0] as ContentPane).Content = null; 但在那种情况下,内存(大小)不会释放。然后我使用 GC.Collect,这次一些内存是空闲的。这是释放内存的写入方式还是任何其他方式,请提供任何参考。而且在这种情况下,当我的用户控件包含 WebBrowser 时,如果我使用 GC.collect,内存不会释放,请也指导我。
谢谢