7

I'm building a Silverlight app which will be deployable to different websites easily, and from within the application, I need to access the domain/host that the Silverlight is hosted at. So, I know for apps within the browser window, this code would perfectly work:

HtmlPage.Document.DocumentUri.Host;

The problem is, I need this app to be installed and be run in out-of-browser mode too. However, when I call the same code in OOB mode, I get an exception, which is perfectly normal as SL in not running in a browser. But logically, the SL app has been installed from somewhere, and I need to access that "origin" URL (or at least, the original host is enough for me in this project). Simply put, if the app is installed from http://example.com/example.xap, how can I access the very string "http://example.com/example.xap" programatically while running out of browser? I won't be knowing this in advance as the app will be modular and be deployed to any domain. Is creating a settings file in isolated storage and setting the domain string if not set the only option, or is there a more trivial way?

Thanks, Can.

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

2 Answers2

12

You can get the address (even in OOB) using Application.Current.Host.Source

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
  • I'm hosting a few web pages on the same web application as my XAP (actually for use in a silverlight WebBrowser control), and this helped me programmatically find the URL. Perfect. – Peter J Jan 12 '12 at 16:39
0

You can acquire the address of the Xap from the BaseAddress of a fresh instance of a WebClient.

 WebClient client = new WebClient();
 string xapAddress = client.BaseAddress;

The BaseAddress is initialised witth the application origin address and will work in an OOB.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306