很多时候,有一个明确的方法,从集合中删除所有项目,这些项目也被处置.
喜欢,
toolStripMenuItem.DropDownItems.Clear();
Run Code Online (Sandbox Code Playgroud)
是足够的,或者我应该这样打电话:
foreach (ToolStripItem item in toolStripMenuItem.DropDownItems)
{
toolStripMenuItem.DropDownItems.Remove(item);
item.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
编辑:好吧ToolStripItem是一个不是问题的例子,对于那些说清楚已经足够的人我发现了另一个例子,TabControl也有项目集合和清除方法.但TabControls可以有复杂的控件(至少我有),需要显式Dispose(即使它们在某些时候由GC自动处理,导致它们占用大量内存).我想最好的答案是divo评论处理项目,然后打电话给清楚.
我在我的应用程序中有一个内存泄漏问题,我用Xamarin.Forms创建.我的应用程序包含带有图像的ListView.如果我点击一个项目并返回到ListPage,我可以在"输出"窗口中看到内存耗尽.我已经打过电话GC.Collect()在OnDisappearing()我ContentPage的.
我base.Dispose()在Android项目中看到了一个.但我不知道如何使用它.
ArticleListPage.xaml
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:NewsArticles.Mobile.Converters;assembly=Something.NewsArticles.Mobile"
xmlns:themes="clr-namespace:NewsArticles.Mobile.Themes;assembly=Something.NewsArticles.Mobile"
x:Class="NewsArticles.Mobile.Pages.ArticlesListPage"
Title="{Binding PageTitle, Mode=OneWay}"
BackgroundColor="{x:Static themes:ColorResources.ArticleListPageBackgroundColor}">
<RelativeLayout>
<ContentPage.Resources>
<ResourceDictionary>
<converters:BooleanNegationConverter x:Key="booleanNegationConverter" />
<converters:StringToImageSourceConverter x:Key="stringToImageSourceConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ListView x:Name="ArticlesList"
StyleId="ArticlesList"
Grid.Row="1"
IsVisible="{Binding IsProcessing, Mode=OneWay, Converter={StaticResource booleanNegationConverter}}">
<ListView.BackgroundColor>
<OnPlatform x:TypeArguments="Color" iOS="Transparent" />
</ListView.BackgroundColor>
<ListView.RowHeight>
<OnPlatform x:TypeArguments="x:Int32" iOS="150" Android="180" WinPhone="170" />
</ListView.RowHeight>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView BackgroundColor="{x:Static themes:ColorResources.ArticleListViewBackgroundColor}">
<ContentView.Padding>
<OnPlatform x:TypeArguments="Thickness"
iOS="10,5"
Android="10,10"
WinPhone="10,10" />
</ContentView.Padding>
<Grid BackgroundColor="White" Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*" /> …Run Code Online (Sandbox Code Playgroud)