Windows Phone 8.1 SL中的内存泄漏具有嵌套自定义控件的Pivot控件

时间:2014-07-17 04:07:00

标签: c# xaml memory-leaks win-phone-silverlight-8.1

我发现如果我在DataTemplate中有一个具有自定义控件的数据透视图,那么每次在数据透视图上滑动都会泄漏~2-3mb。即使自定义控件为空,此问题也会对我产生影响。我的目标是让自定义控件加载图像,但泄漏情况更糟。

如何为在枢轴上不再可见的未实现对象释放内存?

自定义控件的代码隐藏是空的。我已禁用控件上的图像显示,因为我认为这里还有另一个问题,我想在解决第二个问题之前确定一下。

完整代码示例:https://github.com/mpoulson/PivotMemoryLeak

无需加载任何图像的内存配置文件(还没有足够的点来获取图像) https://raw.githubusercontent.com/mpoulson/PivotMemoryLeak/master/Analysis%20-%20No%20Image.png

摄制:

  1. 在调试器中加载示例应用
  2. 加载后,选择查看个人资料按钮
  3. 向左/向右滑动
  4. 请注意每次滑动都会增加内存使用量(示例只有少量配置文件但我希望有> 300且可以使用)。
  5. 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>
    

0 个答案:

没有答案