Tutorials » Silverlight tutorials »

Open a specific xaml page from a Silverlight control


This chapter shows how to open a specific xaml file from a Silverlight control which include multiple xaml pages.


Each Silverlight application project may include multiple xaml pages. When you refer to a .xap file from a web page, the xaml page set using the Application.RootVisual property will be displayed by default.

There may be cases where you want different XAML files to be displayed when you refer to the same .xap file from different web pages. You can do this by passing the xaml file name or some other kind of identification using InitParameters property of the Silverlight control. This property can be set to the Silverlight control from the web page. The value set from web page will be read by the Silverlight control and appropriate xaml page will be opened.

To demonstrate this, let us create a sample Silverlight project with the name "OpenSpecificXaml". (You can read the steps to create a new Silverlight project.)

After you compile and build your project, place the Silverlight control in your web page as shown below:


<asp:Silverlight
InitParameters="PageName=Page1"
ID="Xaml1"
runat="server"
Source="~/ClientBin/OpenSpecificXaml.xap"
MinimumVersion="2.0.30923.0"
Width="100%" Height="100%" />


In the above code, see the code:

 InitParameters="PageName=Page1"


Here we are setting the property "InitParameters" with a key-value pair indicating which xaml page we want to open by default.

Now, we will read this property in the App.xaml file and set the appropriate page to the Application.RootVisual property. Here is the sample code to use in the App.xaml.cs to achieve this:


private void Application_Startup(object sender, StartupEventArgs e)
{
IDictionary parameters = e.InitParams;

if (parameters == null)
{
// No parameter passed.. open the default xaml
this.RootVisual = new DefaultPage();
}
else if (parameters["PageName"] == "Page1")
{
this.RootVisual = new Page1();
}
else if (parameters["PageName"] == "Page2")
{
this.RootVisual = new Page2();
}
else
{
// Open the default xaml
this.RootVisual = new DefaultPage();
}
}


You can download a sample project demonstrating how to pass xaml page name using the InitParameters property.




Next Chapter: Layout controls in Silverlight
Previous Chapter: How to open XAML pages from another XAML page ?
More Chapters: Silverlight Tutorials
More Tutorials: Tutorial Index



Top Contributors
TodayLast 7 Daysmore...

Awards & Gifts
 
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India