Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #41: Using Bookmarks in your Silverlight Application

Bookmarks are very useful in web sites because they allow people to directly navigate back to a section of the web site they visited before. This tip will show you how to apply and use bookmarks in Silverlight applications.

For example, if a user types in http://www.MySilverlightApp.com/default.aspx#Page2 you would intercept this from the address bar and show the second page of your application.

For our demo, for simplicity sakes, we will just make each page it’s own Canvas element with a TextBlock. Ideally you would probably want to make each page its own UserControl. I have created two pages: Page1 and Page2.

Page.xaml:

<UserControl x:Class="SilverlightApplication14.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Canvas>
        <Canvas x:Name="Page1">
            <TextBlock>Hello there this is page 1</TextBlock>
        </Canvas>
        <Canvas x:Name="Page2" Visibility="Collapsed">
            <TextBlock>Hello there this is page 2</TextBlock>
        </Canvas>
    </Canvas>
</UserControl>

In the constructor of Page.xaml.cs we call ProcessBookmark(). This routine will get the bookmark string from the address bar by calling HtmlPage.Window.CurrentBookmark; Once we know what the bookmark is we can set the Visiblity of the Canvas we want displayed to Visible. Note that we also set the title bar of the browser window by calling HtmlPage.Document.SetProperty("title", _page1Title);

public partial class Page : UserControl
{
    const string _page1Title = "My Silverlight App - Page 1";
    const string _page2Title = "My Silverlight App - Page 2";
 
 
    public Page()
    {
        InitializeComponent();
        ProcessBookmark();
    }
 
 
    private void ProcessBookmark()
    {
        string bookMark = HtmlPage.Window.CurrentBookmark;
        switch (bookMark)
        {
            case "Page1":
                HtmlPage.Document.SetProperty("title", _page1Title);
                Page1.Visibility = Visibility.Visible;
                Page2.Visibility = Visibility.Collapsed;
                break;
            case "Page2":
                HtmlPage.Document.SetProperty("title", _page2Title);
                Page1.Visibility = Visibility.Collapsed;
                Page2.Visibility = Visibility.Visible;
                break;
        }
    }
}

So what if you want to set the address bar to a bookmark? You can change the string in the address bar of your browser with the following call: HtmlPage.Window.Eval("window.location.hash='Page1'");

More specifically:

   1: private void SetBookmark(string bookmark)
   2: {
   3:     HtmlPage.Window.Eval("window.location.hash='"+bookmark+"'");
   4: }

Currently if you hit the back button in your browser it will exit you out of your Silverlight. I am looking into a way to intercept the Back and Forward buttons in your browser so that we can create a history of bookmarks internal to our Silverlight application. If anyone has some good resources/techniques let me know and I will incorporate the functionality into this blog.

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Bookmarks are very useful in web sites because they allow people to directly navigate back to a section

# September 16, 2008 6:53 PM

Silverlight Travel » Blog Archiv » Bookmarks in einer Silverlight Anwendung said:

Pingback from  Silverlight Travel  &raquo; Blog Archiv   &raquo; Bookmarks in einer Silverlight Anwendung

# September 17, 2008 12:58 AM

2008 September 17 - Links for today « My (almost) Daily Links said:

Pingback from  2008 September 17 - Links for today &laquo; My (almost) Daily Links

# September 17, 2008 2:16 AM

juergen79 said:

I have troubles with the link of your example. Is this link (www.mysilverlightapp.com/default.aspx correct?

# September 17, 2008 6:13 AM

davidjjon77 said:

Hi Mike,

Very nice - you just removed one item from my to do list. :-)

David Roh

# September 17, 2008 6:39 AM

IanBlackburn said:

I was wondering about using the same technique as the the asp.net 3.5 History support (msdn.microsoft.com/.../cc488553.aspx)

You might even just be able to use the client support provided and use the Html bridge from within Silverlight (msdn.microsoft.com/.../cc488538.aspx)

What do you think?

Ian

# September 17, 2008 7:12 AM

Dew Drop - September 17, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - September 17, 2008 | Alvin Ashcraft's Morning Dew

# September 17, 2008 9:26 AM

Community Blogs said:

Jeff Weber released &quot;Diver&quot;, Mike Snow on Bookmarks and Terence Tsang on 3D Image Space. Want

# September 17, 2008 10:16 AM

Silverlight news for September 17, 2008 said:

Pingback from  Silverlight news for September 17, 2008

# September 17, 2008 10:42 AM

jasonbsteele said:

I used a parameter on the QueryString called "page" and did the page loading in Application_Startup. You may find the use of reflection useful to avoid having to add a case for each page to a switch statement:

private void Application_Startup(object sender, StartupEventArgs e)

       {

           //Save the web pages QueryString

           this.StartParams = (Dictionary<string, string>)System.Windows.Browser.HtmlPage.Document.QueryString;

           if (this.StartParams.ContainsKey("page"))

           {

               string page = this.StartParams["page"];

               // Load Page specified by page parameter

               try

               {

                   UserControl uc = (UserControl)System.Reflection.Assembly.GetExecutingAssembly()

                       .CreateInstance(this.GetType().Namespace + "." + page);

                   this.RootVisual = uc;

               }

               catch

               {

                   //Ignore any error that may be caused by an invalid QueryString

               }

           }

           //If a page was not specified or could not be loaded

           //load the default page

           if (this.RootVisual == null)

               this.RootVisual = new Page();

       }

# September 17, 2008 10:55 AM

mike.snow said:

juergen79 - The link is meant to be a pretend sample link. :) Replace this link with your site.

Schubidu/IanBlackburn - Thanks for the link! I'll check it out.

# September 17, 2008 10:56 AM

Mirrored Blogs said:

Post: Approved at: Sep-18-2008 Silverlight Game: Diver While this Silverlight game can be entertaining

# September 18, 2008 8:04 AM

nosuchthingas2 said:

Here's my favorite example of history & navigation in SL2 using ASP.NET AJAX History.  Great blog too!

jonas.follesoe.no/BackAndForwardNavigationInSilverlight2UsingASPNETAJAX.aspx

# September 18, 2008 8:10 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #46 Title: Font Support in Silverlight Silverlight Tip of the Day #45: Title:

# September 25, 2008 1:56 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:24 AM

??????????????? ??? ?????? « jin_u as blog said:

Pingback from  ??????????????? ??? ?????? &laquo; jin_u as blog

# March 24, 2009 9:58 PM

7 tips for extending browser functionality to Silverlight apps | Igor Ostrovsky Blogging said:

Pingback from  7 tips for extending browser functionality to Silverlight apps | Igor Ostrovsky Blogging

# June 4, 2009 3:50 AM

LetsBlogAbout.NET said:

Silverlight Tips of the Day Update 3

# October 15, 2010 11:01 AM

LetsBlogAbout.NET said:

Silverlight Tips of the Day

# October 15, 2010 11:08 AM