System.Windows.Controls Nam ...


.NET Framework Class Library for Silverlight
Page Class

[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Encapsulates content that can be navigated to by a Frame.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Navigation (in System.Windows.Controls.Navigation.dll)
Syntax

Visual Basic (Declaration)
Public Class Page _
    Inherits UserControl
Visual Basic (Usage)
Dim instance As Page
C#
public class Page : UserControl
XAML Object Element Usage
<navigation:Page .../>
-or-
<navigation:Page ...>
  singleContentElement
</navigation:Page ...>

XAML Values

navigation:

A prefix that is defined to map the XML namespace for the System.Windows.Controls.Navigation assembly and the System.Windows.Controls CLR namespace.

singleContentElement

Exactly one object element for a class that derives from UIElement. This is almost always an object that can take child elements (such as a Panel) so that multiple elements can be added to content. XAML processing of a UserControl has a special behavior that sets this content to the otherwise protected Content property.

Remarks

The Page class enables you to easily create a Silverlight application that supports navigation. You use the Page class to create pieces of content that support navigation from within a Frame. You can create as many different pages as needed to present the content in your application and then navigate to those pages from the frame.

With the Page class, you can support not only navigation to a page of content, but also navigation within that content by providing fragment values. The NavigationContext property provides query string values that you can use to pass values to the page. For example, you may retrieve a query string value to determine which record from a data set to display. When combined with URI mapping, you can create user-friendly URIs that request specific content.

The NavigationService property provides access to the service used by the host to navigate to the page. From within the page, you can navigate to another page or navigate to a new representation of the current page.

You use the NavigationCacheMode property to specify the caching behavior for a page.

You override any of the following methods to add code that responds to navigation events:

NoteNote:

The Page control is available as part of the libraries in the Silverlight Software Development Kit (SDK). For more information, see the Silverlight Tools.

Examples

The following example shows a Silverlight page that retrieves data from a data service and displays that data. The page displays information about a product based on a value in the query string. The OnNavigatedTo method is overridden to obtain a query string value from the NavigationContext object. The NavigationService object for this page is accessed to determine if forward and back navigation is available.

The first part of the example shows the frame that hosts the page and the URI mappings for the request and query string values.

XAML
<navigation:Frame 
       x:Name="ContentFrame" 
       Style="{StaticResource ContentFrameStyle}" 
       Source="/Home" 
       Navigated="ContentFrame_Navigated" 
       NavigationFailed="ContentFrame_NavigationFailed">
    <navigation:Frame.UriMapper>
        <uriMapper:UriMapper>
            <uriMapper:UriMapping 
                Uri="/ProductDetail/{productid}" 
                MappedUri="/Views/ProductDetail.xaml?ProductId={productid}"/>
            <uriMapper:UriMapping 
                Uri="/Reports/{type}/{selection}" 
                MappedUri="/Views/ReportsPage.xaml?type={type}&amp;selection={selection}"/>
            <uriMapper:UriMapping 
                Uri="/{pageName}" 
                MappedUri="/Views/{pageName}.xaml"/>
        </uriMapper:UriMapper>
    </navigation:Frame.UriMapper>
</navigation:Frame>

The second part shows the XAML page that displays the data.

XAML
<navigation:Page x:Class="NavExample.Views.ProductDetail" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d"
     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
     d:DesignWidth="640" d:DesignHeight="480"
     Title="Product Information">
    <StackPanel x:Name="LayoutRoot">
        <ListBox x:Name="ListBox1" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding ProductNumber}"/>
                        <TextBlock Text="{Binding Color}"/>
                        <TextBlock Text="{Binding Size}"/>
                        <TextBlock Text="{Binding ListPrice}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>   
        </ListBox>
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
            <Button Width="100" x:Name="BackNavButton" Click="BackNavButton_Click" 
                    Content="&lt;&lt; back" Visibility="Collapsed" />
            <Button Width="100" x:Name="ForwardNavButton" Click="ForwardNavButton_Click" 
                    Content="forward &gt;&gt;" Visibility="Collapsed" />
        </StackPanel>
    </StackPanel>
</navigation:Page>

The third part shows the code-behind page, which retrieves the data from a data service and determines if navigation buttons are displayed within the page.

Visual Basic
Partial Public Class ProductDetail
    Inherits Page

    Public Sub New()
        InitializeComponent()
    End Sub


    Protected Overrides Sub OnNavigatedTo(ByVal e As NavigationEventArgs)
        GetProductDetail()
        SetButtonVisibility()
    End Sub

    Private Sub SetButtonVisibility()
        If (NavigationService.CanGoBack) Then
            BackNavButton.Visibility = Visibility.Visible
        Else
            BackNavButton.Visibility = Visibility.Collapsed
        End If

        If (NavigationService.CanGoForward) Then
            ForwardNavButton.Visibility = Visibility.Visible
        Else
            ForwardNavButton.Visibility = Visibility.Collapsed
        End If
    End Sub

    Private Sub BackNavButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If (NavigationService.CanGoBack) Then
            NavigationService.GoBack()
        End If
    End Sub

    Private Sub ForwardNavButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If (NavigationService.CanGoForward) Then
            NavigationService.GoForward()
        End If
    End Sub


    Private Sub GetProductDetail()
        Dim productID As String
        Dim svcContext As DataServiceContext

        svcContext = New DataServiceContext(New Uri("AdventureWorks.svc", _
                UriKind.Relative))

        If (Me.NavigationContext.QueryString.ContainsKey("ProductId")) Then
            productID = Me.NavigationContext.QueryString("ProductId")
        Else
            productID = App.Current.Resources("FeaturedProductID").ToString()
        End If

        svcContext.BeginExecute(Of Product)(New Uri("Product(" + productID + ")", _
                UriKind.Relative), AddressOf loadProductCallback, svcContext)
    End Sub
    Private Sub loadProductCallback(ByVal asyncResult As IAsyncResult)
        Dim context As DataServiceContext
        context = asyncResult.AsyncState
        ListBox1.DataContext = context.EndExecute(Of Product)(asyncResult)
    End Sub

End Class
C#
public partial class ProductDetail : Page
{
    public ProductDetail()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        GetProductDetail();
        SetButtonVisibility();
    }

    private void SetButtonVisibility()
    {
        if (NavigationService.CanGoBack)
        {
            BackNavButton.Visibility = Visibility.Visible;
        }
        else
        {
            BackNavButton.Visibility = Visibility.Collapsed;
        }

        if (NavigationService.CanGoForward)
        {
            ForwardNavButton.Visibility = Visibility.Visible;
        }
        else
        {
            ForwardNavButton.Visibility = Visibility.Collapsed;
        }
    }

    private void BackNavButton_Click(object sender, RoutedEventArgs e)
    {
        if (NavigationService.CanGoBack)
        {
            NavigationService.GoBack();
        }
    }

    private void ForwardNavButton_Click(object sender, RoutedEventArgs e)
    {
        if (NavigationService.CanGoForward)
        {
            NavigationService.GoForward();
        }
    }

    private void GetProductDetail()
    {
        string productID;
        DataServiceContext svcContext = 
            new DataServiceContext(new Uri("AdventureWorks.svc", UriKind.Relative));

        if (this.NavigationContext.QueryString.ContainsKey("ProductId"))
        {
            productID = this.NavigationContext.QueryString["ProductId"];
        }
        else
        {
            productID = App.Current.Resources["FeaturedProductID"].ToString();
        }

        svcContext.BeginExecute<Product>(new Uri("Product(" + productID + ")", 
            UriKind.Relative), loadProductCallback, svcContext);
    }

    private void loadProductCallback(IAsyncResult asyncResult)
    {
        DataServiceContext context = asyncResult.AsyncState as DataServiceContext;

        ListBox1.DataContext = context.EndExecute<Product>(asyncResult);
    }
}
Inheritance Hierarchy

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.Control
          System.Windows.Controls..::.UserControl
            System.Windows.Controls..::.Page
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources

Page view tracker