我正在制作一个简单的 Windows Phone 8.1 Silverlight 应用程序。这个想法是,我可以用一张照片(用相机拍摄)进行输入,并为其添加标题和描述文本。保存条目后,主页上会出现一个按钮以查看它。所以我做了 3 个条目,它们列在主页上,但是在导航到他们的页面几次后,我得到了 NavigationFailed 和 OutOfMemoryException。页面很简单,它们只包含一张图片和一些文本块。
我认为问题在于图像仍在内存中,这就是为什么我尝试将它们设置为 null 并强制垃圾收集器,但这根本没有帮助。什么可能导致 OutOfMemory 异常?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string id= "";
if (NavigationContext.QueryString.TryGetValue("id", out id))
{
foreach (cEntry entry in helper.entries)
{
if (entry.id.ToString() == id)
{
textBlock_viewText.Text = entry.text;
textBlock_viewTitle.Text = entry.title;
using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (userStore.FileExists(entry.imageFileName))
{
using (IsolatedStorageFileStream imgStream = userStore.OpenFile(entry.imageFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(imgStream);
image_viewEntryImage.Source = bmp;
bmp = null;
}
}
}
}
}
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
image_viewEntryImage.Source = null;
GC.Collect();
}