2011-02-13 59 views
2

我有具有UserControl稱爲MyBook是,上載,否則會觸發一個後臺線程獲得域名的列表對象每一個URL在Blob存儲託管的Azure的圖像WPF應用程序。圖片不能下載的第二負載在WPF

對於我回來的每個域對象,我添加一個名爲LazyImageControl的自定義控件的新實例,該實例將在後臺下載Azure中的圖像,並在完成後呈現圖像。

這一切正常,但是當我添加第二個MyBook控制圖像不負載由於某些原因的情景,我想不通這是爲什麼。

下面是LazyImageControl

public LazyImageControl() 
    { 
     InitializeComponent(); 

     DataContextChanged += ContextHasChanged; 
    } 

    private void ContextHasChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     // Start a thread to download the bitmap... 
     _uiThreadDispatcher = Dispatcher.CurrentDispatcher; 
     new Thread(WorkerThread).Start(DataContext); 
    } 

    private void WorkerThread(object arg) 
    { 
     var imageUrlString = arg as string; 
     string url = imageUrlString; 

     var uriSource = new Uri(url); 
     BitmapImage bi; 
     if (uriSource.IsFile) 
     { 
      bi = new BitmapImage(uriSource); 
      bi.Freeze(); 
      _uiThreadDispatcher.Invoke(DispatcherPriority.Send, new DispatcherOperationCallback(SetBitmap), bi); 
     } 
     else 
     { 
      bi = new BitmapImage(); 
      // Start downloading the bitmap... 
      bi.BeginInit(); 
      bi.UriSource = uriSource; 
      bi.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.Default); 
      bi.DownloadCompleted += DownloadCompleted; 
      bi.DownloadFailed += DownloadFailed; 
      bi.EndInit(); 
     } 

     // Spin waiting for the bitmap to finish loading... 
     Dispatcher.Run(); 
    } 

    private void DownloadFailed(object sender, ExceptionEventArgs e) 
    { 
     throw new NotImplementedException(); 
    } 

    private void DownloadCompleted(object sender, EventArgs e) 
    { 
     // The bitmap has been downloaded. Freeze the BitmapImage 
     // instance so we can hand it back to the UI thread. 
     var bi = (BitmapImage)sender; 
     bi.Freeze(); 

     // Hand the bitmap back to the UI thread. 
     _uiThreadDispatcher.Invoke(DispatcherPriority.Send, new DispatcherOperationCallback(SetBitmap), bi); 

     // Exit the loop we are spinning in... 
     Dispatcher.CurrentDispatcher.InvokeShutdown(); 
    } 

    private object SetBitmap(object arg) 
    { 
     LazyImage.Source = (BitmapImage)arg; 
     return null; 
    } 

代碼以使這個問題是,第一次的WorkerThread運行正常後這樣做,但我從來沒有得到一個回調到DownloadCompletedDownloadFailed方法,我沒有想法爲什麼...

任何想法?

回答

1

不知道,但也許你應該嘗試設置BitmapImage.UriSource應觸發圖像的加載之前安裝的DownloadCompletedDownloadFailed事件處理程序,所以它可能是在你的事件處理程序連接(不是第一個加載它因爲那裏的加載需要一段時間,但然後圖像被緩存,並會立即加載)

另外:LazyImageControl從哪個類繼承,所以我可以測試它,如果不是這樣?

+0

我想試試,LazyImageControl擴展用戶控件,它只是一個圖像 – Mark 2011-02-13 21:45:05