Access host or URL info in Silverlight out-of-browser
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 m开发者_开发百科ode 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.
You can get the address (even in OOB) using Application.Current.Host.Source
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.
精彩评论