C# 如何从 XAML 引用嵌入式资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9419611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refer to Embedded Resources from XAML?
提问by eric.itzhak
I have several images that i want to be Embedded into the exe.
我有几个图像要嵌入到 exe 中。
When i set the Build Actionto Embedded ResourceI get through out the code an error that the Resource isn't available and asking me to set the Build Action to Resource
当我将Build Action设置为Embedded Resource 时,我通过代码得到了一个错误,即 Resource 不可用,并要求我将 Build Action 设置为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'
So, What am i doing wrong?
那么,我做错了什么?
采纳答案by yo chauhan
When you set the BuildActionto Resourceit goes as embedded resource in an assembly.
Or you can set BuildActionto Contentthen it will bundled into the resulting .xap file.
You can use any one of these BuildActions. By setting BuildActionto Contentyou can access Image like:
"/Resources/Images/darkaurora.png"(must begin with slash). And when you use the BuildAction Resourcethen you can access image as "/YearBook;component/Resources/Images/darkaurora.png"(assemblyname;component/relativepath). Hope this will help.
当您将BuildAction设置为Resource 时,它会作为程序集中的嵌入资源。或者,您可以将BuildAction设置为Content,然后它将捆绑到生成的 .xap 文件中。您可以使用这些 BuildAction 中的任何一种。通过将BuildAction设置为Content,您可以像这样访问图像:(
"/Resources/Images/darkaurora.png"必须以斜杠开头)。当您使用BuildAction Resource 时,您可以以"/YearBook;component/Resources/Images/darkaurora.png"(assemblyname;component/relativepath) 的形式访问图像。希望这会有所帮助。
回答by devdigital
Set the build action to Resource, not Embedded Resource
将构建操作设置为资源,而不是嵌入式资源
回答by devdigital
ImageSourcecannot be instantiated.
ImageSource无法实例化。
public abstract class ImageSource : Animatable,
IFormattable
There's that little abstractin there which will screw your day up. Your xaml is actually trying to instantiate an instance of ImageSource, then assign the value within the element (your Uri, in this case) to a property marked with the ContentPropertyAttribute (??) using whatever converter that could be located to convert the string to an object (again, ??).
里面的东西很少abstract,会让你的一天搞砸。您的 xaml 实际上是在尝试实例化 ImageSource 的一个实例,然后使用可以定位的任何转换器将元素中的值(在本例中为您的 Uri)分配给标有 ContentPropertyAttribute (??) 的属性以将字符串转换为一个对象(再次,??)。
I think you want a BitmapSource.
我想你想要一个BitmapSource。
<BitmapImage
x:Key="Image_Background"
UriSource="/Images/darkaurora.png" />
回答by t.shikanai
Just for those using xamarin forms and bump into this question, this can be done by creating a custom xaml markup extension explained here:
对于那些使用 xamarin 表单并遇到这个问题的人来说,这可以通过创建一个自定义的 xaml 标记扩展来完成,这里解释如下:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows
in the "Embedded Images"->"Using XAML" section
在“嵌入图像”->“使用 XAML”部分
Citation of custom extension
引用自定义扩展
[ContentProperty (nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue (IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return imageSource;
}
}
citation of how to use
使用方法的引用
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
x:Class="WorkingWithImages.EmbeddedImagesXaml">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<!-- use a custom Markup Extension -->
<Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" />
</StackLayout>
</ContentPage>

