2013-04-22 32 views
8

在我的应用程序中,我使用下面提到的辅助方法将我的独立存储图像绑定到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 _)

+0

您在列表视图中有多少张图片?他们多大?你可以在你的应用程序上运行内存分析(在Visual Studio中调试 - >启动Windows Phone应用程序分析 - >性能分析 - >内存)并发布结果? – Haspemulator 2013-04-22 17:55:49

+0

尝试使用LongListSelector作为平面列表 – Mahantesh 2013-04-23 07:37:36

+2

@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

回答

0

ListBox内的高速缓存可能会占用您的内存,这对于较大的图像尤其明显。我不熟悉您发布的帮助程序方法,但尝试添加此方法。

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

    //rest of the code ... 
} 
0

好的,我花了一些时间回到这个问题。我会在这里分享我的发现,但我并不认为他们是问题的真正答案,而是一种解决方法。不过,我希望它能帮助别人。

首先我想确认OutOfMemoryException发生在某些情况下。但是,令人惊讶的是,这取决于您使用的页面布局。事实上,如果你的布局涉及StackPanel,你会有一个例外。我想,这归结于MeasureOverrideArrangeOverride方法在StackPanel中实现的事实(虽然我可能在这里完全错误)。它看起来像ListBoxStackPanel的孩子,它试图在显示之前加载所有图像。这当然会导致内存泄漏。

另一方面,如果您使用类似Grid的东西作为图像列表的父项,则不存在这样的例外,并且内存负载是合理的。

这里的页面布局,为我工作:

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

这是最好的答案,我现在有你。请让我知道它是否有帮助。

+0

我认为这不是问题。我也试过类似的东西, 2013-04-24 06:38:23

+0

我也尝试了上面提到的方法,但结果是一样的。 :( – 2013-04-24 06:40:37

0

你可以这样试试,Stream对象会自动处理。

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; 
     } 
    } 
} 
相关问题