Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The given System.Uri cannot be converted into a Windows.Foundation.Uri

I'm trying to programmatically load a BitmapImage in a XAML Metro app. Here's my code:

var uri = new Uri("/Images/800x600/BackgroundTile.bmp", UriKind.RelativeOrAbsolute);
var imageSource = new BitmapImage(uri);

The second line crashes with a System.ArgumentException:

The given System.Uri cannot be converted into a Windows.Foundation.Uri. Please see http://go.microsoft.com/fwlink/?LinkID=215849 for details.

The link just goes to the MSDN home page, so it's no use.

I've also tried removing the leading /, in case WinRT has different expectations about relative URIs, but I still get the same exception.

Why am I getting this exception for what seems to be a perfectly valid URI?

like image 701
Joe White Avatar asked Sep 27 '11 13:09

Joe White


3 Answers

In the Consumer Preview, the correct URL format has apparently changed to ms-appx:/Images/800x600/BackgroundTile.bmp

like image 161
Sander Avatar answered Oct 14 '22 22:10

Sander


Judging from the documentation for Windows.Foundation.Uri, it looks like WinRT doesn't support relative URIs. I tried a pack:// URI, but that gave me a UriFormatException, so apparently that's not the way to do it in WinRT either.

I found the answer on this thread: MS invented yet another URI format for WinRT resources. This works:

new Uri("ms-resource://MyAssembly/Images/800x600/BackgroundTile.bmp")

Note that you don't add your actual assembly name -- the MyAssembly part is literal text.

like image 33
Joe White Avatar answered Oct 14 '22 23:10

Joe White


You will need to use the page's BaseUri property or the image control's BaseUri property like this:

//using the page's BaseUri property
BitmapImage bitmapImage = new BitmapImage(this.BaseUri,"/Images/800x600/BackgroundTile.bmp");
image.Source = bitmapImage;

or

//using the image control's BaseUri property
image.Source = new BitmapImage(image.BaseUri,"/Images/800x600/BackgroundTile.bmp");

you can see the reason and solution here

like image 3
Sunday Okpokor Avatar answered Oct 14 '22 23:10

Sunday Okpokor