2014-10-08 27 views
0

我有一個列表視圖,我想要顯示從URL加載的每個項目的圖像。如果我使用ImageCell作爲DataTemplate,它會加載一次,然後如果我嘗試再次加載,我會得到一個OutOfMemory錯誤。當在列表視圖中使用圖像時Xamarin.forms OutOfMemory錯誤

我明年受審的代碼介紹Xamarin窗體頁這裏http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/創建一個自定義ViewCell

class MyViewCell : ViewCell 
{ 
    public MyViewCell() 
    { 
     var image = new MyImage 
     { 
      HorizontalOptions = LayoutOptions.Start 
     }; 
     image.SetBinding(Image.SourceProperty, new Binding("ImagePath")); 
     image.WidthRequest = image.HeightRequest = 40; 

     var nameLayout = CreateLayout(); 

     var viewLayout = new StackLayout() 
     { 
      Orientation = StackOrientation.Horizontal, 
      Children = { image, nameLayout } 
     }; 
     View = viewLayout; 
    }//end constructor 

    static StackLayout CreateLayout() 
    { 

     var titleLabel = new Label 
     { 
      HorizontalOptions= LayoutOptions.FillAndExpand, 
      TextColor = Color.FromRgb(0, 110, 128) 
     }; 
     titleLabel.SetBinding(Label.TextProperty, "Title"); 

     var detailLabel = new Label 
     { 
      HorizontalOptions = LayoutOptions.FillAndExpand 
     }; 
     detailLabel.SetBinding(Label.TextProperty, "Detail"); 

     var layout = new StackLayout() 
     { 
      HorizontalOptions = LayoutOptions.StartAndExpand, 
      Orientation = StackOrientation.Vertical, 
      Children = { titleLabel, detailLabel } 
     }; 
     return layout; 
    } 

} 

但這不會加載即使加載列表中的第一次

我試過Avrohom的代碼(在這裏找到Xamarin.Forms ListView OutOfMemoryError exception on Android),但不幸的是它仍然不會第一次加載。

有沒有人有任何想法?

+0

你的問題很不清楚。我看不出問題標題和描述之間的關係。 – 2014-10-08 11:44:50

+0

對不起 - 已編輯,不知道我是如何管理的 – user1667474 2014-10-08 12:26:11

回答

2

您的圖片可能太大,而不是文件大小的意義,但這些文件的位圖表示。

您應該使用較小的圖像,或者實現一個自定義單元格渲染器,以便在運行時爲您執行此操作。請參閱Xamarin文章Load Large Bitmaps Efficiently

+0

感謝您回答Stephane。圖片來自網址,並且它們看起來都是500px plus。我如何去調整大小? (對不起,對於C#和Xamarin來說是非常新的 - 就像幾個星期以後)。我知道如何實現自定義渲染器,但我不知道從哪裏開始調整大小。歡呼 – user1667474 2014-10-08 12:37:03

+0

列表視圖中的500px圖像的確非常巨大,並且需要時間來檢索。你爲什麼不把它們調整到你的後端?或者使用像cloudinary.com這樣的服務? – 2014-10-08 15:03:58

+0

感謝您的意見,Stephane。這是一個uni項目,所以我們將不得不將它們排除在外,但我會在未來謹記它。 – user1667474 2014-10-09 00:38:22