[英]wpf Image resources and visual studio 2010 resource editor
我對這個問題的動機實際上只是通過ImageSource的依賴屬性指定要在用戶控件中使用的圖像。 我正在遇到一些涉及管理,訪問和單元測試的痛點。
不,資源編輯器不是一個好的工具。
在WPF應用程序中,最好的方法是將所有圖像放在“圖像”目錄中,並將每個圖像標記為“資源”。 然后,您可以直接在圖像控件和其他地方引用它們。
以下是精確的步驟:
現在,您可以在XAML中輕松引用圖像:
<Image Source="Images/MyImage.png" />
或者在代碼中:
var source = (BitmapSource)Application.LoadComponent(
new Uri("Images/MyImage.png", UriKind.Relative));
您還可以在外部裝配中引用圖像:
<Image Source="ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png" />
在代碼中將是:
var source = (BitmapSource)Application.LoadComponent(
new Uri("ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png",
UriKind.Relative));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.