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);