WPF - Return image from converter using pack URI
I would like to create a WPF converter to return a ce开发者_如何转开发rtain image depending on a boolean value.
I have so far got the following code:
return (bool) value
? new BitmapImage(new Uri("pack://application:,,,MyApp.ApplicationResources;component/Resources/image1.png", UriKind.Absolute))
: new BitmapImage(new Uri("pack://application:,,,MyApp.ApplicationResources;component/Resources/image2.png", UriKind.Absolute));
However this gives me the exception
System.Windows.Markup.XamlParseException occurred
Message=The URI prefix is not recognized.
The Images are in a referenced Assembly of : MyApp.ApplicationResources in a folder "Resources" and both are set to Content Type: Resource.
If I use the same URI in source property of an image in xaml it works fine!
Any ideas? Thanks
Looks like you are missing a /
in your Uri
:
return (bool) value
? new BitmapImage(new Uri("pack://application:,,,/MyApp.ApplicationResources;component/Resources/image1.png", UriKind.Absolute))
: new BitmapImage(new Uri("pack://application:,,,/MyApp.ApplicationResources;component/Resources/image2.png", UriKind.Absolute));
精彩评论