Navigation Application
The Silverlight Navigation feature provides a rapid
means of developing page-based web applications. The two primary
controls in the System.Windows.Control.Navigation namespace are Frame
and Page. The Frame control hosts a single Page control utilizes the
Navigation APIs to allow the user to navigate between the pages. The
Page control resembles the commonly used UserControl control to hold the
contents of the respected page.In this tutorial, we will show you to have create a quick Navigation Application.
Create a New Project
- Select New Project from the File Menu
- Select Silverlight Navigation Application template.
- Name the project NavigationApplication and click OK.
- In the New Silverlight Application dialog, uncheck the Host the Silverlight application in a new Web site checkbox.
- The project generates the default template and files for a sample page-based application. The Views Folder has pre-generated pages (AboutPage, ErrorWindow, and HomePage). App.xaml has the application-wide styles used to design the application. MainPage.xaml has the default implementation of the application with sample data.
- Compile and run the project. The application opens in the default
web browser. Click on the navigation links to test out their
functionality. The browser stores the current address of the
application's frame in the address bar. Another benefit of the
navigation feature is its integration with the browser's history.
Access the browser's back history to view the application's history.
Extending the Project
The default project has two default pages (About and Home). To add additional pages, you can create pages using the Silverlight Page template and add the buttons to call them in MainPage.xaml We will extend this process by data binding the navigation links to a local xml file.- Create the Navigation.xml file with the following XML content.
<navigation> <page name="Home" url="/Views/HomePage.xaml" /> <page name="About" url="/Views/AboutPage.xaml" /> </navigation>
- Add System.Xml.Linq to the project's references.
- Add System.Xml.Linq namespace to MainPage.xaml.cs.
- Create the NavigatePage class. This class will be used to store the data from the XML file.
public class NavigationPage
{
public string Name { get; set; }
public string Url { get; set; }
}
- The navigation list is rendered in MainPage.xaml.
The static version needs to be replaced with an ItemsControl that is
data bounded to the contents of the XML file. Find the following from MainPage.xaml.
<StackPanel Style="{StaticResource NavigationPanelStyle}">
<Button Click="NavButton_Click" Tag="/Views/HomePage.xaml" Content="home" Style="{StaticResource PageLinkStyle}"/>
<Button Click="NavButton_Click" Tag="/Views/AboutPage.xaml" Content="about" Style="{StaticResource PageLinkStyle}"/>
</StackPanel>
<ItemsControl x:Name="listNavigation">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Style="{StaticResource NavigationPanelStyle}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Click="NavButton_Click" Tag="{Binding Url}" Content="{Binding Name}" Style="{StaticResource PageLinkStyle}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
- Add a Loaded event to the constructor that points to the MainPage_Loaded function.
- Load the Navigation.xml file and data bind it to the listNavigation control with the following code snippet.
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
XElement xml = XElement.Load("Navigation.xml");
List<NavigationPage> pages = (from page in xml.Elements("page")
select new NavigationPage
{
Name = (string)page.Attribute("name"),
Url = (string)page.Attribute("url")
}).ToList();
listNavigation.ItemsSource = pages;
SetPage(pages[0].Url);
}
- Create the SetPage function that calls the Navigation Framework.
- Compile and run the project. The output should remain the same.
void SetPage(string page)
{
this.Frame.Navigate(new Uri(page, UriKind.Relative));
}
Add the Links Page
- Right-click on the Views folder in the Solution Explorer and select Add New Item.
- Select Silverlight Page in the Add Item dialog box and name it LinksPage.xaml.
- Add the following code snippet to LinksPage.xaml.
<Grid Background="White">
<StackPanel>
<TextBlock Text="Links" Style="{StaticResource HeaderTextStyle}"/>
<HyperlinkButton Content="http://www.silverlighttoys.com" NavigateUri="http://www.silverlighttoys.com" Style="{StaticResource HyperlinkButtonStyle}"/>
</StackPanel>
</Grid>
- Add the new page entry to Navigation.xml.
<page name="Links" url="/Views/LinksPage.xaml" />
- Compile and run the project. Click on the Links hyperlink to test it out.
No comments:
Post a Comment