2013-04-22 39 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; 
     } 
    } 
} 
相關問題