在我的应用程序中,我使用下面提到的辅助方法将我的独立存储图像绑定到Image控件。我从这个链接辅助方法,“Binding Image stored in the Isolated Storage to Image Control in 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);
}
}
});
}
}
}
我用这一个ListBox控件中。如果尝试使用默认库图像,一切都将按预期工作。但如果我尝试使用大尺寸图片(通过设备相机拍摄),应用程序会崩溃。
而且这里是我得到
类型“System.OutOfMemoryException的”发生在System.Windows.ni.dll的一个例外,但在用户代码中没有处理的异常
堆栈跟踪
在MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM() 在MS.Internal.XcpImports.BitmapSource_SetSource(的BitmapSource的BitmapSource,CValue &字节流) 在System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(流流在System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource) 在System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource) MyaPP.Common.IsoStoreImageSource。 <> c__DisplayClass4。 <> c__DisplayClass6.b__1(Object _)
您在列表视图中有多少张图片?他们多大?你可以在你的应用程序上运行内存分析(在Visual Studio中调试 - >启动Windows Phone应用程序分析 - >性能分析 - >内存)并发布结果? – Haspemulator 2013-04-22 17:55:49
尝试使用LongListSelector作为平面列表 – Mahantesh 2013-04-23 07:37:36
@Haspemulator:这里提到的问题“http://stackoverflow.com/questions/15700340/out-of-memory-exception-while-loading-images-from-isolated-storage” ,“http://blogs.developpeur.org/kookiz/archive/2013/02/17/wpdev-memory-leak-with-bitmapimage.aspx”,我如何用你的实现来解决这个问题。 – 2013-04-23 11:51:25