如何从 XAML 引用嵌入式资源? [英] How to refer to Embedded Resources from XAML?

查看:45
本文介绍了如何从 XAML 引用嵌入式资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个图像要嵌入到 exe 中.

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

当我将 Build Action 设置为 Embedded Resource 时我通过代码得到一个错误,即资源不可用,并要求我将构建操作设置为 Resource

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>

此代码位于资源文件中.但没有一个工作,他们都抛出这个错误:

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.

在代码的不同地方我得到:

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'

那么,我做错了什么?

推荐答案

当您将 BuildAction 设置为 Resource 时,它会作为程序集中的嵌入资源.或者,您可以将 BuildAction 设置为 Content,然后它将捆绑到生成的 .xap 文件中.您可以使用这些 BuildAction 中的任何一种.通过将 BuildAction 设置为 Content,您可以像这样访问图像:"/Resources/Images/darkaurora.png"(必须以斜线开头).当您使用 BuildAction Resource 时,您可以通过 "/YearBook;component/Resources/Images/darkaurora.png" (assemblyname;component/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.

这篇关于如何从 XAML 引用嵌入式资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆