如何释放Windows Phone 8 App中数据模板中图像的内存? [英] How to free memory hold by images in data template in windows phone 8 App?

查看:18
本文介绍了如何释放Windows Phone 8 App中数据模板中图像的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 longlistselector,我有一个数据模板,用于定义要添加到列表中的项目类型.数据模板有一个图像控件,其源与路径动态绑定,因此列表中的每个项目都有一个关联的图像控件.我面临的问题是这些 Image 控件永远不会释放它们占用的内存,从而导致内存不足异常.在正常情况下,我设置 bitmapImage.UriSource=null 以释放与位图关联的内存但在这种情况下找不到这样做的方法.这里是 longlistselector 的 xaml 代码和与之关联的数据模板..

I have a longlistselector for which I have a data template that defines the type of items to be added to the list. The data template has an Image control whose source is binded with path dynamically, thus each item in list has an associated Image Control. The problem I face is that these Image control never free the memory they occcupy resulting in out of memory exception. On normal scenario I set bitmapImage.UriSource=null to deallocate the memory associated with the bitmap But can't find a way to do so in this scenario. Here is the xaml code for the longlistselector and the data template associated with it..

数据模板

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="MediaItemTemplate">      
        <Canvas VerticalAlignment="Top">
            <Border BorderBrush="#FF4791CA" BorderThickness="3">
                <Image Height="100" Width="100" VerticalAlignment="Top" Grid.RowSpan="2" Stretch="UniformToFill">
                    <Image.Source> 
                          <BitmapImage UriSource="{Binding path}" CreateOptions="BackgroundCreation"  DecodePixelHeight="50" DecodePixelWidth="50"/>
                    </Image.Source>
                </Image>
             </Border>
             <Image Source="/Icons/check.png" Height="16" Width="16" Grid.Row="0" Grid.Column="0" VerticalAlignment="Top" Margin="80,7,7,0" Canvas.ZIndex="100" OpacityMask="Black" Visibility="{Binding visibility}" Name="checkImage" >    
             </Image>
          </Canvas> 
      </DataTemplate>
</phone:PhoneApplicationPage.Resources>

LonglistSelector

<phone:LongListSelector 
            Tap="ListMedia_Tap"
            x:Name="ListMedia"
            HorizontalAlignment="Left"
            Height="624" 
            VerticalAlignment="Top"
            Width="436"

背景=透明"

ItemTemplate="{StaticResource MediaItemTemplate}"LayoutMode="Grid" GridCellSize="120,120"/>

ItemTemplate="{StaticResource MediaItemTemplate}" LayoutMode="Grid" GridCellSize="120,120"/>

我对 windows 手机编程很陌生,基本上我想做的是开发一种图像浏览器体验.请帮我解决释放内存的方法.如果我完全做错了,请纠正我或提出更好的方法来实现相同的功能.提前谢谢...

I am very new to windows phone programming, What basically I want to do is to develop kind of image browser experience. Please Help me out with ways to deallocate the memory. In case I am doing it completely wrong please correct me or suggest better ways to achieve the same functionality. Thanx in advance...

推荐答案

我发现处理这种情况的解决方案是制作自定义控件以自动将 urisource 设置为 null:

A solution I've found to handle this case is making a custom control to automatically set the urisource to null:

public class SafePicture : System.Windows.Controls.ContentControl
{
    public SafePicture()
    {
        this.Unloaded += this.SafePictureUnloaded;
    }

    private void SafePictureUnloaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var image = this.Content as System.Windows.Controls.Image;

        if (image != null)
        {
            image.Source = null;
        }
    }
}

然后,只需将所有图片都包裹在该控件中:

Then, just wrap all your pictures in that control:

<my:SafePicture>
    <Image Source="{Binding Path=path}" />
</my:SafePicture>

这篇关于如何释放Windows Phone 8 App中数据模板中图像的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆