如何从XAML请参阅嵌入式资源?嵌入式、资源、XAML

2023-09-04 00:18:44 作者:雨后的咖啡,回忆起你在时

我有我想要被嵌入到exe多张图像。

I have several images that i want to be Embedded into the exe.

当我将生成操作为嵌入的资源 我打通了C时错误的资源不可用$ C $和要求我设置生成操作为资源

When i set the Build Action to Embedded Resource I get through out the code an error that the Resource isn't available and asking me to set the Build Action to Resource

我试了几种不同的方法:

I Tried several different methods :

 <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>

这code坐落在一个资源文件。 但没有工作,他们都抛出此错误:

This code sits in a Resource file. But none worked, they all throw this error :

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.

和在code不同的地方,我得到:

And in different places in code i get :

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'

那么,什么是我做错了吗?

So, What am i doing wrong?

推荐答案

在您设置的的BuildAction 为资源它会在程序集嵌入的资源。 或者你也可以将的BuildAction 到内容,然后它会捆绑到生成的.xap文件。 可以使用这些BuildActions中的任何一个。通过设置的BuildAction 为内容您可以访问图片,如: /资源/图像/ darkaurora.png(必须以斜杠)。而当你使用 BuildAction的资源,然后你可以访问图像作为年鉴;组件/资源/图像/ darkaurora.png(的AssemblyName;组件/ relativepath)。希望这会有所帮助。

When you set the BuildAction to Resource it goes as embedded resource in an assembly. Or you can set BuildAction to Content then it will bundled into the resulting .xap file. You can use any one of these BuildActions. By setting BuildAction to Content you can access Image like: "/Resources/Images/darkaurora.png" (must begin with slash). And when you use the BuildAction Resource then you can access image as "YearBook;component/Resources/Images/darkaurora.png" (assemblyname;component/relativepath). Hope this will help.