Windows phone 7 应用程序在尝试绑定独立存储映像时崩溃

Windows phone 7 应用程序在尝试绑定独立存储映像时崩溃,windows-phone-7,windows-phone-8,windows-phone,Windows Phone 7,Windows Phone 8,Windows Phone,在我的应用程序中,我使用下面提到的helper方法将我的独立存储映像绑定到映像控件。我从链接“”获得此帮助程序方法 } 我在ListBox控件中使用它。如果尝试使用默认的库映像,一切都将按预期工作。但是如果我尝试使用大尺寸的图像(通过设备摄像头拍摄),应用程序就会崩溃 这是我得到的例外 System.Windows.ni.dll中发生“System.OutOfMemoryException”类型的异常,但未在用户代码中处理 堆栈跟踪 在MS.Internal.FrameworkCallbacks

在我的应用程序中,我使用下面提到的helper方法将我的独立存储映像绑定到映像控件。我从链接“”获得此帮助程序方法

}

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

这是我得到的例外

System.Windows.ni.dll中发生“System.OutOfMemoryException”类型的异常,但未在用户代码中处理

堆栈跟踪

在MS.Internal.FrameworkCallbacks.notifyManagedeBuggeronativeoom()处 位于MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource BitmapSource、CValue和ByTestStream) 位于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__显示类4.c__显示类6.b___1(对象)

列表框中的缓存可能会占用您的内存,对于较大的图像,这一点尤其明显。我不熟悉您发布的助手方法,但请尝试添加此方法

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

    //rest of the code ...
}

好吧,我花了一些时间才回到这个问题上来。我将在这里分享我的发现,但我并不认为它们是问题的真正答案,而是一种解决办法。然而,我希望它能帮助一些人

首先,我想确认
OutOfMemoryException
在某些情况下会发生。但是,令人惊讶的是,这取决于您使用的页面布局。事实上,如果您的布局涉及到
StackPanel
,您将有一个例外。我想,这归结于
MeasureOverride
ArrangeOverride
方法是如何在
StackPanel
中实现的(尽管我在这里可能完全错了)。当
ListBox
StackPanel
的子对象时,它会尝试在显示之前加载所有图像。当然,这会导致内存泄漏

另一方面,如果您使用类似于
Grid
的内容作为图像列表的父对象,则不会出现此类异常,并且内存负载是合理的

以下是对我有用的页面布局:

<Grid>
    <ListBox ItemsSource="{Binding IsoStorePics}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image local:IsoStoreImageSource.IsoStoreFileName="{Binding Path}" Margin="5"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>


这是我现在给你的最好答案。如果有帮助,请告诉我。

您可以这样尝试,流对象将自动处理

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;
         }
     }
}

您的列表视图中有多少图像?它们有多大?您能否在应用程序上运行内存分析(
Debug->在Visual Studio中启动Windows Phone应用程序分析->评测->内存
)并发布结果?请尝试将LongListSelector用作平面List@Haspemulator:这里提到的问题是“”,我如何通过您的实施来解决这个问题。我也面临着同样的问题@Haspemulator,我执行了内存分析,得到了上面博客中提到的类似响应。你能更新这个问题的解决方案吗?我认为这不是问题所在。我也尝试过类似的方法,也尝试过你提到的上述方法,但结果是一样的(
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;
         }
     }
}