Nothing but .net

Silverlight: How to do navigation easily

Here's a trick to a question I have heard a few times already: how to perform easy navigation in Silverlight 1.1?
This problem can be partially solved using hidden panels: you click on a button, Panel1 is hidden and Panel2 becomes visible. This is a trick that is often done in ASP.net too.
Well, this trick can also be done in Silverlight, using the Control class. This class makes it possible to have a piece of XAML that has been loaded dynamically, act as a Control, that can be shown or hidden to do navigation (or actually, fake navigation).
Consider the following code. I have a panel, Panel1, that inherits from Control. Inside the constructor, XAML is read from a resource (ie, a XAML file).

public class Panel1 : Control

    {

        FrameworkElement myPanel;

        Rectangle button;

        public Panel1()

        {

            System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("App1.MyPanel.xaml");

            button = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());

            button = (Rectangle)myPanel.FindName("button");

        }

    }

 

Now, in the main code behind page, we'll instantiate the constructor of the Panel1 class, and we'll add the controls it generates dynamically (from the XAML that is) to the existing XAML.


Panel1 p = new Panel1();

this.Children.Add(p);


If we later want to remove the panel, the following line does the trick.

this.Children.Remove(p);


Posted on Sunday, 30 September 2007 07:49 by gill  |  Comments (4)
Filed under:   Tips and Tricks | Silverlight

Related posts

Comments

October 4. 2007 02:13

trackback

Trackback from WynApse

Silverlight Cream for October 3, 2007 -- # 99

WynApse

March 20. 2008 12:09

vamsi

Hi,

I am trying to implement navigation between two xaml files.

I need to traverse from one page to another after firing event from button.

How? any idea.

pls mail me..

vamsi

September 15. 2008 04:53

Trisha

Thanks for the example..

Trisha

November 30. 2008 22:45

mahesh a

hi vasmi,
you can use the RootVisual property, Please have look


private void Navigation_Click(object sender, RoutedEventArgs e) {
App.Current.RootVisual = new NewPage();
}

mahesh a

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

December 1. 2008 11:42