Delen via


Page.OnNavigatedFrom(NavigationEventArgs) Method

Definition

Invoked immediately after the Page is unloaded and is no longer the current source of a parent Frame.

protected:
 virtual void OnNavigatedFrom(NavigationEventArgs ^ e) = OnNavigatedFrom;
void OnNavigatedFrom(NavigationEventArgs const& e);
protected virtual void OnNavigatedFrom(NavigationEventArgs e);
function onNavigatedFrom(e)
Protected Overridable Sub OnNavigatedFrom (e As NavigationEventArgs)

Parameters

e
NavigationEventArgs

Event data that can be examined by overriding code. The event data is representative of the navigation that has unloaded the current Page.

Remarks

OnNavigatedFrom is invoked by default when you navigate between pages in your app. You can override this method and get info about the navigation from the NavigationEventArgs, and take any action required by your app. However, you should avoid changing the state of the page by unregistering event handlers or doing other cleanup, especially in apps that call Frame.GetNavigationState on suspend.

On suspending

In addition to being invoked when navigating between pages, the OnNavigatedFrom method is invoked when the app is suspended if Frame.GetNavigationState is called in the Application.Suspending event handler.

While it seems natural to use OnNavigatedFrom to unregister event handlers that were registered in OnNavigatedTo, this can lead to problems if your app saves its state on suspend. Saving state on suspend typically involves calling Frame.GetNavigationState, which then calls Page.OnNavigatedFrom on the current page, which signals the page to persist/serialize any state it retains on back navigation or rehydration. If the app is suspended and then resumed (without a termination and relaunch), any event handlers unregistered in OnNavigatedFrom won't be re-registered because Page.OnNavigatedTo isn't called on resume.

To avoid this issue, you should instead use OnNavigatingFrom or the Unloaded event, which are only called on actual navigation, to unregister event handlers and do other cleanup.

Applies to