DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Windows Phone 7 Tip Of The Day: Call resources by their absolute path

Denzel D. user avatar by
Denzel D.
·
Mar. 04, 11 · · Interview
Like (0)
Save
Tweet
8.16K Views

Join the DZone community and get the full member experience.

Join For Free

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):

App.Current.Host.GetType();

In my case, I get this:

file:///Applications/Install/4FFA38B5-00AF-4760-A7EB-7C0C0BC1D31A/Install/

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.

Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • Top Soft Skills to Identify a Great Software Engineer
  • Choosing Between GraphQL Vs REST
  • Synchronization Methods for Many-To-Many Associations

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo