我有幾個圖像,我想嵌入到EXE中。如何從XAML引用嵌入式資源?
當我生成操作設置爲嵌入的資源 我打通了代碼中的錯誤,該資源不可用,要我生成操作設置爲資源
我嘗試了幾種不同的方法:
<ImageSource x:Key="Image_Background">YearBook;component/Resources/Images/darkaurora.png</ImageSource>
<ImageSource x:Key="Image_Background">Images/darkaurora.png</ImageSource>
<ImageSource x:Key="Image_Background">pack://application:,,,/Resources/Images/darkaurora.png</ImageSource>
此代碼坐落在一個資源文件。 但沒有工作,他們都拋出這個錯誤:
Cannot convert the string 'pack://application:,,,/Resources/Images/darkaurora.png' into a 'System.Windows.Media.ImageSource' object. Cannot locate resource 'resources/images/darkaurora.png'. Error at object 'Image_Background' in markup file 'YearBook;component/Resources/ImageResources.xaml' Line 4 Position 6.
,並在代碼不同的地方,我得到:
the file 'YearBook;component/Resources/Images/shadowdrop.png' is not a part of the project or its 'Build Action' property is not set to 'Resource'
那麼,什麼是我做錯了什麼?
「嵌入式資源」和「資源」是不同的。如果您在Reflector或ILSpy中查看生成的程序集,則會看到它們以不同的方式包含在內。作者問如何用「嵌入式資源」而不是「資源」來做到這一點。 – BrainSlugs83 2013-11-01 23:19:23
@ethicallogics但如果需要通過viewmodel綁定這個資源呢?有沒有其他的選擇,比綁定到字符串路徑? – 2015-08-21 10:17:24
這可惜不適用於BuildAction =資源 – Nick 2015-12-01 16:05:21