I have a .png file as the content of my application. When I bind it in haml like this
<ImageBrush x:Key="BtnBackImageBrush" ImageSource="/Assets/Images/back.png" />
everything is fine.
I read this article , and when I try to access this .png programmatically, I get an error.
The code I'm using is:
Uri baseUri = new Uri("ms:appx"); Image img = new Image(); img.Source = new BitmapImage(new Uri(baseUri, "/Assets/Images/back.png")); img.ImageFailed += (sender, args) => new MessageDialog("Error").ShowAsync();
And my question is, how do I reference the source image files that are packaged into my metro style app?
Thanks for the tips.
UPDATE: I found the answer! We need to set baseUri using the parent FrameworkElement, instead of setting it manually. For example:
// Usage myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content"); public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path) { var uri = new Uri(parent.BaseUri, path); BitmapImage result = new BitmapImage(); result.UriSource = uri; return result; }
Thank you for the article .
azhidkov
source share