How to refer to Embedded Resources from XAML?
Asked Answered
R

3

32

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

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'

So, What am i doing wrong?

Redmund answered 23/2, 2012 at 19:12 Comment(1)
Set the build action to Resource, not Embedded ResourceBustard
V
35

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.

Voltameter answered 23/2, 2012 at 19:34 Comment(5)
"Embedded Resource" and "Resource" are different. If you look at the generated assembly in Reflector or ILSpy you will see that they get included in different ways. The author asked how to do it with "Embedded Resource", not "Resource".Terat
@ethicallogics but what if need to bind this resource via viewmodel? Is there any other option, than bind to string path?Tammeratammi
You need a leading slash on the assemblyname when BuildAction is Resource: "/YearBook;component....". See https://mcmap.net/q/80270/-storing-wpf-image-resourcesGulf
I think it should be "pack://application:,,,/Resources/Images/darkaurora.png" in the case of Resource as a BuildActionThorr
Since this question has been tagged as WPF, this question about Resource vs. Embedded Resource seems relevant: stackoverflow.com/questions/1934826Downing
V
1

Just for those using xamarin forms and bump into this question, this can be done by creating a custom xaml markup extension explained here:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows

in the "Embedded Images"->"Using XAML" section

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>
Visualize answered 1/7, 2019 at 4:20 Comment(0)
B
-1

ImageSource cannot be instantiated.

public abstract class ImageSource : Animatable, 
IFormattable

There's that little abstract in 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, ??).

I think you want a BitmapSource.

<BitmapImage 
    x:Key="Image_Background" 
    UriSource="/Images/darkaurora.png" />
Beckett answered 23/2, 2012 at 19:22 Comment(2)
Actually you got this one wrong, you can declare ImageSources in XAML just fine because the class has a type converter associated with it (see the attributes in the docs). Can't tell you where that is documented but i think you can trust me on that. You get that error only because the inner-XML of the tag is missing.Stanwin
@H.B.: Ouch. You're right. It converts a string into a BitmapFrame (assuming the string is a valid Uri pointing to an image file somewhere).Beckett

© 2022 - 2024 — McMap. All rights reserved.