2015-06-09 14 views
1

此使用:
發送Uri直接作爲給構造的參數設置對象photoUriSourceBitmap.UriSource不獲取設置

BitmapImage photo = new BitmapImage(new Uri("pack://application:,,,/Images/EmptyImage.jpg")); 

enter image description here

不起作用:

但設置UriSource屬性保持UriSource爲空

BitmapImage photo = new BitmapImage(); 
photo.UriSource = new Uri("pack://application:,,,/Images/EmptyImage.jpg"); 

enter image description here

回答

2

根據MSDN

BitmapImage.UriSource必須在BeginInit在/ EndInit塊。

因此,你需要這樣設置:

BitmapImage photo = new BitmapImage(); 
photo.BeginInit(); 
photo.UriSource = new Uri("pack://application:,,,/Images/EmptyImage.jpg"); 
photo.EndInit(); 
+0

天哪!爲什麼我沒有讀到:P。理想情況下,它應該拋出異常,而不是忽略它。 – Marshal