Welcome to AspAdvice Sign in | Join | Help

Silverlight Hyperlink

Playing with Silverlight and needed a simple way to redirect the page.  This isn't built-in yet but probably will be.  For now, you can call out to the page's script, and for this since I don't want to rely on the page having any particular function defined, I'm just using Eval() and passing in what I need.

So, in a button MouseLeftButtonDown() handler, I've got:

HtmlPage.Window.Eval("document.location.href='" + url + "';");

*Update* - This is in fact built in:

HtmlPage.Window.Navigate(new Uri(urlString));
// or
HtmlPage.Window.Navigate(new Uri(urlString), targetString);
// or
HtmlPage.Window.Navigate(new Uri(urlString), targetString, targetFeaturesString);

That's it.  That one will let the user Back to the current page, which was what I wanted.  If you use location.replace('some url'); instead it will actually eliminate the current page in the browser history and replace it with the new URL, so if you prefer that behavior, use that (might be good on a CheckOut page so the user can't checkout twice).

Now what I would like, to go along with this, is a way to get parameters passed into the silverlight appilcation from its calling page via the path used.  For instance, I might refer to my silverlight application as "app.xap?url=http://aspalliance.com" and then in my silverlight application I need a way to get to that url variable in the XAP file's "querystring".  I still want access to the host page's QueryString collection, too, though.  So it needs to be a different bag of parameter values.  Not sure what we'll see here, yet.

Update: Found this as well.  To get at the QueryString of the XAP package, use this:

Uri sourceUri = new Uri(App.Current.Host.Source.OriginalString);
string querystring = sourceUri.Query;

Parsing the Querystring is an exercise for the user.  HttpUtility.ParseQueryString (in System.Web) is not ported over to Silverlight.  So you have to roll your own.  I was going to use an extension method to add it but since HttpUtility is itself a static class, you can't extend it.  Thus, I created a new static class, HttpExtensions, and added a ParseQueryString() method to it.  Further, NameValueCollection doesn't appear to exist in Silverlight either, but System.Collections.Generic.Dictionary<TKey,TVal> does so I used one of those:

Dictionary<string, string> queryStringDictionary = HttpExtensions.ParseQueryString(querystring);

For the code for ParseQueryString, I simply grabbed the (now open source) contents of System.Web.HttpUtility.ParseQueryString.

Published Wednesday, January 16, 2008 7:59 PM by ssmith
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Silverlight Hyperlink

I assume you're using Silverlight 1.0, so this might not help you, but there are easy ways to do these things in Silverlight 1.1/2.0.

You can navigate to another page using System.Windows.Browser.HtmlPage.Navigate("url").  Also, you can access the page's query string through the System.Windows.Browser.HtmlPage.QueryString dictionary.

Wednesday, January 16, 2008 9:54 PM by Matt Casto

# re: Silverlight Hyperlink

I'm aware of the access to the page's querystring - that's not what I want.  I want a querystring for the XAP file just like I get today with a .SWF file in flash.

As for HtmlPage.Navigate(), thanks, I'd missed that!  I'll have to give that a shot (I actually asked how to do that to some MSFT folks and they sent me down the javascript path so I didn't look any further in the API).

Thursday, January 17, 2008 12:32 AM by ssmith

# re: Silverlight Hyperlink

I'm not sure what I'm missing here, but what's a .XAP file?

Thursday, January 17, 2008 11:19 AM by Matt Casto

# Silverlight Cream for January 17, 2008 -- #172

Video Interview with Flash Guru Jesse Warden, Tim Heuer discusses issues surrounding moving files around

Thursday, January 17, 2008 12:29 PM by Community Blogs

# re: Silverlight Hyperlink

A .XAP file is a ZIP file with a different extension, as you can see in ScottGu's recent post on SL 2.0:

http://weblogs.asp.net/scottgu/archive/2008/02/22/first-look-at-silverlight-2.aspx?CommentPosted=true#commentmessage

Friday, February 22, 2008 1:16 PM by ssmith

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below