在我的應用程序中,我使用下面提到的輔助方法將我的獨立存儲圖像綁定到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