Reputation: 5544
I have a lot of images on my project that's why I created a Images resource at my solution I referenced all those images as this:
Then I reference at my XAML as this:
<Image
x:Name="ImgFail"
Grid.Row="1"
Grid.ColumnSpan="2"
Source="{x:Static img:Imagenes.IMG_FailStamp}"/>
This way it throw an error on run time Isn't a valid value for 'source'property
But if directly reference the image as this:
<Image
x:Name="ImgOk"
Grid.Row="1"
Grid.ColumnSpan="2"
Source="/||||||||.Recursos.Imagenes;component/Resources/Ok-PNG-Pic.png"/>
It works.
How can i do to set Image source properly using static resources ?
Upvotes: 1
Views: 3061
Reputation: 101633
That's because when you do
Source="{x:Static img:Imagenes.IMG_FailStamp}"
This is compiled rougly like this:
img.Source = Imagenes.IMG_FailStamp
But Image.Source
is of type ImageSource
and your IMG_FailStamp
is of type string
- this does not compile. But when you do this:
Source="/||||||||.Recursos.Imagenes;component/Resources/Ok-PNG-Pic.png"
it's different - now your string will go through the ImageSourceConverter
and it will convert it to valid ImageSource
(so will read your file and create source from it).
So when you use x:Static
- you just tell directly what value to use and no converters are involved. So if you want to use x:Static
- target property should be of type ImageSource
.
If you still want to store paths to images in resources, do the following:
Open resources.resx file and change access modifier at the top from "internal" to "public". This will make Imagenes.IMG_FailStamp
public property.
Bind to it like this:
Source="{Binding Source={x:Static img:Imagenes.IMG_FailStamp}, Mode=OneTime}"
Binding will run the necessary converter and image will display fine.
Upvotes: 3