在我的应用程序中,我使用下面提到的辅助方法将我的独立存储图像绑定到图像控件。我从链接“获得了这个辅助方法将存储在独立存储中的图像绑定到 Windows Phone 中的图像控件 https://stackoverflow.com/questions/16102651/binding-image-stored-in-the-isolated-storage-to-image-control-in-windows-phone/16103494#comment23023219_16103494"
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.Windows.ni.dll 中发生“System.OutOfMemoryException”类型的异常,但未在用户代码中处理
堆栈跟踪
在 MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM()
在 MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)
在System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(流streamSource)
在System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(流streamSource)
在System.Windows.Media.Imaging.BitmapSource.SetSource(流streamSource)
在 MyaPP.Common.IsoStoreImageSource.c__DisplayClass4.c__DisplayClass6.b__1(Object _)