Condividi tramite


Page.OnNavigatedTo Method

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

Called when a page becomes the active page in a frame.

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

Syntax

'Declaration
Protected Overridable Sub OnNavigatedTo ( _
    e As NavigationEventArgs _
)
protected virtual void OnNavigatedTo(
    NavigationEventArgs e
)

Parameters

Remarks

You override the OnNavigatedTo method to examine the navigation request and prepare the page for display. For example, you can load the requested data and enable or disable visual elements.

Typically, you use the OnNavigatedTo method instead of creating an event handler for the Loaded event. The OnNavigatedTo method is preferable because it is only called once for each time the page becomes active. The Silverlight framework raises the Loaded event each time the element is added to the visual tree, which potentially can happen more than once when activating a page.

The OnNavigatedTo method is called for each request, even when the page is retrieved from the cache. You should include in this method code that must be executed for each request rather than placing that code in the Page constructor.

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.