2013-03-25 30 views
4

我試圖下載一個圖像並在事件結束時觸發。我用這個:BitmapImage.ImageOpened Not Firing

BitmapImage btest = new BitmapImage(new Uri("http://www.google.com/images/srpr/logo4w.png")); 
btest.ImageOpened += btest_ImageOpened; 

void btest_ImageOpened(object sender, RoutedEventArgs e) 
{ 
    throw new NotImplementedException(); 
} 

但是,ImageOpened事件不會觸發。如果我將圖像控件的源設置爲BitmapImage使用:

image.Source = btest; 

它確實會觸發。爲什麼ImageOpened事件不會觸發,除非BitmapImage發件人被設置爲圖片的來源?

+1

我猜 - 它從來沒有用過 - 因此它從來沒有加載或打開 - 只是一個想法,但有道理我認爲 – NSGaga 2013-03-25 22:07:37

+0

你是對的。事實證明,BitmapImages不會加載,直到他們需要。 – msbg 2013-03-25 22:17:11

+0

你還希望我發表一個答案 - 如果它讓你做正確的事情 - 所以你可以上/關 – NSGaga 2013-03-25 22:25:49

回答

5

我自己想通了。默認情況下,BitmapImage將不會被初始化,直到有必要。 BitmapImage的CreateOptions的默認值是BitmapCreateOptions.DelayCreation。解決此問題所需的全部內容是將CreateOptions設置爲BitmapCreateOptions.None

我的工作代碼爲:

BitmapImage btest = new BitmapImage(new Uri("http://www.google.com/images/srpr/logo4w.png")); 
btest.CreateOptions = BitmapCreateOptions.None; 
btest.ImageOpened += btest_ImageOpened; 

void btest_ImageOpened(object sender, RoutedEventArgs e) 
{ 
    throw new NotImplementedException(); 
} 
+1

** BitmapImage的CreateOptions的默認值是BitmapCreateOptions.None。所有你需要做的事情就是將CreateOptions設置爲BitmapCreateOptions.None。** 嗯,所以CreateOptions應該設置爲None? – 2013-04-18 17:03:29

+0

我無法得到這個工作 - 也許因爲btest.CreateOptions = BitmapCreateOptions.None已經是默認值。還有其他解決方案嗎? – Cam 2013-07-17 03:33:07

+1

根據文檔不是默認值。 BitmapCreateOptions.DelayCreation是默認值。將此更改爲「無」也不起作用。 – 2013-09-11 18:25:42

0

(我只是張貼此基礎上我們的討論 - 因爲它有助於OP得到正確的解決方案)

我猜 - 這是從來沒有使用過 - 所以從來沒有加載或打開 - 只是一個 想法,但很有意義,我認爲

+0

謝謝,並沒有完全解決問題,但肯定讓我在正確的方向思考。 – msbg 2013-03-25 22:35:38

+0

非常感謝你會 - 如果沒有其他:) - NP,不客氣 – NSGaga 2013-03-25 22:40:09

0

在我的Windows手機8.0 Silverlight應用程序我得到的事件ImageOpened當我將創建選項設置爲 BitmapCreateOptions.BackgroundCreation

設置爲

BitmapCreateOptions.DelayCreation時(這是默認值)

BitmapCreateOptions.None

0

我的兩分錢,也許它可以幫助別人......配售的圖像控制我不明白這一點在頁面上佈線,任何xaml中的事件和源代碼都可以正常工作,並且事件觸發。但是,當我在代碼中加載位圖圖像並設置圖像控件的源代碼時,雖然圖像加載正常,但圖像控件的事件不會啓動。我嘗試了上面提到的所有位圖選項,但沒有一個似乎起作用。我最終處理了位圖圖像的ImageOpened事件,而不是處理事件觸發的圖像控件。請注意,雖然在此階段圖像控件尚未完全加載圖像,因此您必須參考位圖圖像以獲取詳細信息,而不是圖像控件。