Page.NavigationContext Property

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

Gets an object that contains information about the navigation request.

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

Syntax

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

Property Value

Type: System.Windows.Navigation.NavigationContext
An object that contains information about the navigation request.

Remarks

When you retrieve an instance of the NavigationContext class through the NavigationContext property, you use the NavigationContext object to obtain information about the navigation request, such as the query string values and uniform resource identifier. You can use these values to generate the desired state of a page for a navigation request. For example, you may retrieve a query string value to determine which record from a data set to display.

Examples

The following example shows how to override the OnNavigatedTo method in a Silverlight page and obtain a query string value from the NavigationContext object. The query string value is used to determine which product is retrieved from a data service and displayed in the page.

Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
    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
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    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.