Share via


Page.NavigationService Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the service that the host used to navigate to this page.

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

Syntax

'Declaration
Public ReadOnly Property NavigationService As NavigationService
public NavigationService NavigationService { get; }

Property Value

Type: System.Windows.Navigation.NavigationService
The service the host used to navigate to this page.

Remarks

To navigate from within a page to either another page or another representation of the current page, you need to integrate the page navigation with the host navigation. You retrieve the host navigation for the current page through the NavigationService property.

Examples

The following example shows a Silverlight page that contains forward and back navigation buttons within the page. To determine if forward or back navigation is allowed, the page accesses the navigation service and uses its properties.

The first part of the example shows the XAML page.

<!-- NOTE: 
  By convention, the sdk prefix indicates a URI-based XAML namespace declaration 
  for Silverlight SDK client libraries. This namespace declaration is valid for 
  Silverlight 4 only. In Silverlight 3, you must use individual XAML namespace 
  declarations for each CLR assembly and namespace combination outside the scope 
  of the default Silverlight XAML namespace. For more information, see the help 
  topic "Prefixes and Mappings for Silverlight Libraries". 
-->
<sdk:Page x:Class="NavExample.Views.ProductDetail" 
     xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d"
     xmlns:sdk="https://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
     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>
</sdk:Page>

The second part shows the code-behind page.

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

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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