Partilhar via


NavigationContext.QueryString Property

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

Gets a collection of query string values.

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

Syntax

'Declaration
Public Property QueryString As IDictionary(Of String, String)
public IDictionary<string, string> QueryString { get; private set; }

Property Value

Type: System.Collections.Generic.IDictionary<String, String>
A collection that contains the query string values.

Remarks

You retrieve values from the query string to generate the desired state of a page for a navigation request. A navigation request with query string values includes the query string key/value pairs after a question mark (?) delimiter. For example, you can store in the query string the identifier of the record you wish to display on a page, such as /Views/Product.xaml?productid=45&format=long. The values from the query string are decoded before being stored in the QueryString collection.

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.