我发现如果我在DataTemplate中有一个具有自定义控件的数据透视图,那么每次在数据透视图上滑动都会泄漏~2-3mb。即使自定义控件为空,此问题也会对我产生影响。我的目标是让自定义控件加载图像,但泄漏情况更糟。
如何为在枢轴上不再可见的未实现对象释放内存?
自定义控件的代码隐藏是空的。我已禁用控件上的图像显示,因为我认为这里还有另一个问题,我想在解决第二个问题之前确定一下。
完整代码示例:https://github.com/mpoulson/PivotMemoryLeak
无需加载任何图像的内存配置文件(还没有足够的点来获取图像) https://raw.githubusercontent.com/mpoulson/PivotMemoryLeak/master/Analysis%20-%20No%20Image.png
摄制:
Page.xaml
<phone:PhoneApplicationPage
x:Class="PanoramaApp1.Views.ProfilePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:PanoramaApp1.ViewModels"
xmlns:controls="clr-namespace:PanoramaApp1.Controls"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<shell:SystemTray.ProgressIndicator>
<shell:ProgressIndicator IsIndeterminate="True" Text="Loading..." />
</shell:SystemTray.ProgressIndicator>
<phone:PhoneApplicationPage.Resources>
<vm:ProfileCollectionViewModel x:Key="viewModel"/>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock Text="{Binding TargetProfile.ScreenName}"
FontSize="{StaticResource PhoneFontSizeMedium}"/>
</DataTemplate>
<DataTemplate x:Key="ItemTemplate">
<controls:WindowsPhoneControl1 Visibility="Visible"/>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--TitlePanel contains the name of the application and page title-->
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" MaxHeight="80"/>
</Grid.RowDefinitions>
<phone:Pivot
x:Name="ProfilePivot"
Grid.Row="0"
DataContext="{StaticResource viewModel}"
ItemsSource="{Binding Profiles}"
SelectionChanged="ProfilePivot_SelectionChanged"
HeaderTemplate="{StaticResource HeaderTemplate}"
ItemTemplate="{StaticResource ItemTemplate}" />
</Grid>
</phone:PhoneApplicationPage>
WindowsPhoneControl1.xaml
<UserControl x:Class="PanoramaApp1.Controls.WindowsPhoneControl1"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--This make the leak even worse!!
<Image x:Name="Image" Source="{Binding TargetProfile, Converter={StaticResource ProfileBackgroundImageConverter}}" />-->
</Grid>
</UserControl>