Export (0) Print
Expand All

NavigationService.Navigating Event

Occurs when a new navigation is requested.

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

'Declaration
Public Event Navigating As NavigatingCancelEventHandler
'Usage
Dim instance As NavigationService 
Dim handler As NavigatingCancelEventHandler 

AddHandler instance.Navigating, handler
You cannot use this event in XAML.

Navigating is raised when a new navigation is requested, but before the source content is requested, including when:

  • Navigate is called.

  • GoBack or GoForward is called (or an entry is selected from a navigation UI).

  • A content fragment is navigated to.

You handle Navigating if you need to discover pertinent information regarding the navigation request before navigation starts. This information is available from the NavigatingCancelEventArgs object that is passed to the Navigating event handler.

A WebRequest object for the navigation is created and available from the NavigatingCancelEventArgs parameter; because the actual request hasn't been made at this point, you can reconfigure the WebRequest object, if necessary.

You also handle NavigatingCancelEventArgs to cancel navigation, if required, by setting Cancel to true. If you cancel navigation, no other navigation events are raised.

NoteNote:

If your application is hosted in the browser, you cannot prevent the user from navigating away from your application by canceling the Navigating event.

After the first piece of content is navigated to by a navigator (NavigationWindow, Frame), each piece of content that is navigated away from is added to navigation history. If you need to save state information about the content you are navigating away from, you can add the state to the journal entry for that content by setting ContentStateToSave with a CustomContentState object.

NoteNote:

When NavigationService raises Navigating, it also raises Application.Navigating event on the Application object.

The following example shows how to handle Navigating to detect whether a request to refresh static content has been made and, if so, to cancel the request.

Navigating.

void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
    // Don't allow refreshing of a static page 
    if ((e.NavigationMode == NavigationMode.Refresh) &&
        (e.Uri.OriginalString == "StaticPage.xaml"))
    {
        e.Cancel = true;
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2014 Microsoft