NavigationContext Class

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

Represents the state of a navigation operation.

Inheritance Hierarchy

System.Object
  System.Windows.Navigation.NavigationContext

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

Syntax

'Declaration
Public NotInheritable Class NavigationContext
public sealed class NavigationContext

The NavigationContext type exposes the following members.

Properties

  Name Description
Public property QueryString Gets a collection of query string values.

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

You use the NavigationContext class to retrieve the query string values for a navigation request. With the query string values, you can generate the desired state of a page for a navigation request. For example, you can store in the query string the identifier of the record you wish to display on a page.

To access an instance of the NavigationContext class, you use the NavigationContext property of the Page class.

Examples

The following example shows how to override the OnNavigatedTo 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.

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.