2010-03-09 32 views
5

我有自定義對象的集合稱爲DataItems包含圖像0​​的URI的,我想加載,並把收集在我的Silverlight應用程序中使用。如何通過WebClient請求傳遞信息來識別被加載的對象?

當我處理每個DataItem的,我得到它的SourceUri(例如 「http://..../picture001.png」),並啓動它裝載

void LoadNext() 
{ 
    WebClient webClientImgDownloader = new WebClient(); 
    if (loadedItemIndex < RegisteredDataEntries.Count()) 
    { 
     DataItem dataItem = RegisteredDataEntries[registeredIdCodes[loadedItemIndex]]; 
     if (dataItem.Kind == DataItemKind.Image) 
     { 
      webClientImgDownloader.OpenReadCompleted += 
       new OpenReadCompletedEventHandler(webClientImgDownloader_OpenReadCompleted); 
      webClientImgDownloader.OpenReadAsync(new Uri(dataItem.SourceUri, 
       UriKind.Absolute)); 
      webClientImgDownloader.AddObject(dataItem); //???????????????????? 
      webClientImgDownloader.Headers["idCode"] = dataItem.IdCode; //????????????? 
      webClientImgDownloader.ResponseHeaders["idCode"] = dataItem.IdCode; //????????????? 
     } 
    } 
    else 
    { 
     OnLoadingComplete(this, null); 
    } 
} 

然後,當該圖像的加載已完成,我救

void webClientImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 
    dataItemIdCode = e.DataItem.IdCode; //??????????????????? 
    dataitemIdCode = ((DataItem)sender).IdCode; //????????????????????? 

    BitmapImage bitmap = new BitmapImage(); 
    bitmap.SetSource(e.Result); 

    Image image = new Image(); 
    image.Source = bitmap; 
    Images.Add(dataItemIdCode, image); 
} 

:在集合圖像但是,如何將當前DataItem的IdCode傳遞給我的OpenReadCompleted方法,以便當該圖像完成加載後,我還可以根據其IdCode識別它?

答:

弗郎的建議的作品,這裏是爲別人尋找這::

webClientImgDownloader.OpenReadAsync(new Uri(dataItem.SourceUri, 
    UriKind.Absolute), dataItem); 
... 
DataItem dataItem = e.UserState as DataItem; 

回答

6

有一個OpenDataAsync重載需要用戶令牌對象的行。您應該在OpenReadCompletedEventArgs,UserState屬性(繼承自AsyncCompletedEventArgs)中獲取此對象。

+0

到底我在找什麼,謝謝 – 2010-03-09 17:42:05

相關問題