尝试绑定隔离存储映像时应用程序崩溃

Nit*_*aul 8 windows-phone-7 windows-phone windows-phone-8

在我的应用程序中,我使用下面提到的帮助方法将我的独立存储图像绑定到Image控件.我从这个链接辅助方法," 绑定存储在独立存储图像控制在Windows Phone的图片 "

public class IsoStoreImageSource : DependencyObject
{
public static void SetIsoStoreFileName(UIElement element, string value)
{
    element.SetValue(IsoStoreFileNameProperty, value);
}
public static string GetIsoStoreFileName(UIElement element)
{
    return (string)element.GetValue(IsoStoreFileNameProperty);
}

// Using a DependencyProperty as the backing store for IsoStoreFileName.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsoStoreFileNameProperty =
    DependencyProperty.RegisterAttached("IsoStoreFileName", typeof(string), typeof(IsoStoreImageSource), new PropertyMetadata("", Changed));

private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    Image img = d as Image;

    if (img != null)
    {
        var path = e.NewValue as string;
        SynchronizationContext uiThread = SynchronizationContext.Current;

        Task.Factory.StartNew(() =>
        {
            using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoStore.FileExists(path))
                {
                    var stream = isoStore.OpenFile(path, System.IO.FileMode.Open, FileAccess.Read);
                    uiThread.Post(_ =>
                    {
                        var _img = new BitmapImage();
                        _img.SetSource(stream);
                        img.Source = _img;
                    }, null);
                }
            }
        });               
    }
}
Run Code Online (Sandbox Code Playgroud)

}

我在ListBox控件中使用它.如果尝试使用默认库图像,一切都将按预期工作.但是,如果我尝试使用大尺寸的图像(通过设备相机拍摄),应用程序会崩溃.

这是我得到的例外

System.Windows.ni.dll中出现"System.OutOfMemoryException"类型的异常,但未在用户代码中处理

堆栈跟踪

在MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM()在MS.Internal.XcpImports.BitmapSource_SetSource(的BitmapSource的BitmapSource,CValue&字节流)在System.Windows.Media.Imaging在System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(流StreamSource的). BitmapImage.SetSourceInternal(流的StreamSource)在System.Windows.Media.Imaging.BitmapSource.SetSource(流的StreamSource)在MyaPP.Common.IsoStoreImageSource.<> c__DisplayClass4.<> c__DisplayClass6.b__1(对象_)

key*_*rdP 0

内部的缓存ListBox可能会占用您的内存,这对于较大的图像尤其明显。我不熟悉您发布的辅助方法,但尝试添加它。

if (img != null)
{
    BitmapImage bitmapImage = img.Source as BitmapImage;
    bitmapImage.UriSource = null;
    img.Source = null;

    //rest of the code ...
}
Run Code Online (Sandbox Code Playgroud)