2016-02-21 16 views
0

我有一個視圖,我有一個列表框。嘗試使用url-s中的約5000張圖片填充它。我有這些圖像的列表,但是當我嘗試綁定它時,我必須等待很多時間才能看到圖像。它非常緩慢地顯示圖像。如何從url綁定大量的圖像wpf c#?

public static FileDto MapToFileDto(this FileUploadedResponzeJsonDto resp) 
{ 
    var dto = new FileDto(); 

    dto.Delete_url = resp.Delete_url; 
    dto.File_ID = resp.File_ID; 
    dto.FileName = resp.Name; 
    dto.FileSizeInBytes = resp.FileSizeInBytes; 
    dto.Info_url = resp.Info_url; 
    dto.Url = resp.Url; 
    dto.Visits = resp.Visits; 

    if(!string.IsNullOrEmpty(resp.Surl) && resp.Name.HasImageExtension()) 
    dto.DisplayImage = new System.Windows.Media.Imaging.BitmapImage(new Uri(string.Format(ClientConstans.API_URL_GET_SMALL_IMAGE, resp.Surl))); 

    return dto; 
} 

這使得URL-S這樣的:

string.Format(ClientConstans.API_URL_GET_SMALL_IMAGE, resp.Surl) 
// https://mysite/image.png 

我充滿FileDto列表,並將其綁定到列表框。它快速創建列表,但是我必須等待很多圖像。

如果我想立即展示圖片,我應該如何填充圖片列表框? (他們真的很小的圖像,大約4kb /圖像)

而我有另一個問題,我不能打破連接。當我清除列表並嘗試重新填充時,我無法做到這一點。它引發我只是一個例外,因爲URI綁定。

+0

嘗試將圖像加載到後臺線程上並同時下載圖像 – User2012384

回答

0
private async void LoadImageAsync(object sender, System.Windows.RoutedEventArgs e) 
{ 
    await Task.Delay(100); 
    var senderItem = sender as Image; 

    if (senderItem != null) 
    { 
     var dataContext = senderItem.DataContext as FileDto; 
     if (dataContext != null) 
     { 
      if (!string.IsNullOrEmpty(dataContext.ImageUrl)) 
      { 
       var fullFilePath = dataContext.ImageUrl; 

       BitmapImage bitmap = new BitmapImage(); 
       bitmap.BeginInit(); 
       bitmap.UriSource = new Uri(fullFilePath, UriKind.Absolute); 
       bitmap.EndInit(); 

       senderItem.Source = bitmap; 
      } 
     } 

    } 

} 

我在圖像控件的Loaded事件異步載入圖像。它非常快速,只使用一點存儲器和處理器。

+0

這是發件人項目。我將一個位圖圖像進行圖像處理並將其傳遞給發送方圖像控件的源代碼。 ..哦,你的意思是var image = new ...?這不需要,這是一個嘗試,對不起我的壞。 :) –