应用程序在尝试绑定隔离存储映像时崩溃 [英] App crashes while trying to bind the Isolated Storage Image
问题描述
在我的应用程序中,我使用下面提到的辅助方法将我的隔离存储图像绑定到图像控件.我从链接将独立存储中存储的图像绑定到 Windows Phone 中的图像控件"
In my application I am using the below mentioned helper method for binding my Isolated storage image to Image control. I got this helper method from the link "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 控件中使用它.如果尝试使用默认库图像,一切都会按预期工作.但是如果我尝试使用大尺寸的图像(通过设备相机拍摄),应用程序就会崩溃.
I am using this inside a ListBox control. And if try with default library images everything will work as expected. But if I try with the images with large size( taken through device camera ) the app crashes.
这是我得到的例外
System.Windows.ni.dll 中出现类型为System.OutOfMemoryException"的异常,但未在用户代码中处理
An exception of type 'System.OutOfMemoryException' occurred in System.Windows.ni.dll but was not handled in user code
堆栈跟踪
在 MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM()在 MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)在 System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)在 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 _)
at MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM() at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream) at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource) at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource) at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource) at MyaPP.Common.IsoStoreImageSource.<>c__DisplayClass4.<>c__DisplayClass6.b__1(Object _)
推荐答案
你可以这样试试,Stream对象会自动释放.
You can try like this, Stream object will automatically disposed.
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(imagePath))
{
using (Stream imagestream = new IsolatedStorageFileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, iso))
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(imagestream);
imgControl.Source = bmp;
}
}
}
这篇关于应用程序在尝试绑定隔离存储映像时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!