Windows Phone 7 Tip Of The Day: Call resources by their absolute path
There are situations when developers need to call specific resources only by their absolute path. Of course, this situation mostly applies to cases when web resources are invoked (on Windows Phone 7 this is extremely fenced), but can also be applied to local (built-in) entities. So what do you do in this case?
a) host the resource somewhere and then invoke it
b) access it locally via an absolute path
If you answered a, then you are thinking in the wrong context here - Windows Phone 7 won't allow the developer to invoke remote resources directly through an Uri (e.g. to set an image). The second method doesn't seem quite plausible, though.
The limitation here consists in the fact that the actual application path (relative to the phone file system) is hidden by default - there is no way it can be obtained by using standard methods - the developer is limited to the existing resource access layer and the isolated storage. This, however, doesn't mean that undocumented features can't be used. Chris Walshie discovered that the default App.Current.Host implementation carries a hidden property, that shows the path to the application folder.
A simple method call for it will reveal it (given that a breakpoint is set at the work line):
In my case, I get this:
Remember that the GUID will ultimately change depending on the application.
So, if I want to access a specific image from my Resources folder (given that it's copied there and is set to be of type Content), I can use a snippet like this:
string imagePath = "file:///Applications/Install/4FFA38B5-00AF-4760-A7EB-7C0C0BC1D31A/Install/Resources/dice.png";
BitmapImage image = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
testImage.Source = image;
There is one but here. Try guessing whether this will work or not:
XDocument doc = XDocument.Load("file:///Applications/Install/4FFA38B5-00AF-4760-A7EB-7C0C0BC1D31A/Install/Resources/feed.xml");
The answer is no - an exception will be thrown anyway:
BitmapImage doesn't really see a problem downloading an image, therefore it is possible to use an absolute URL to an online resource (image) and it will still accept it. On the other hand, XDocument is restricted to local resources and therefore will only accept a relative URI as a possible parameter.