Click here to Skip to main content
13,352,879 members (48,868 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 4 Oct 2012

How to Cancel the Navigation in Windows Phone Programatically

, 5 Oct 2012
Rate this:
Please Sign up or sign in to vote.
A simple tip to allow you to cancel navigation


When you are navigating from one page to another in a Windows Phone app, there might be a scenario where you want to get a confirmation message from the user and act based on it. If the user is OK with it, then the navigation might continue or else we might have to cancel the navigation.

You can implement this kind of logic in the OnNavigatingFrom event that provides the NavigatingCancelEventArgs and includes properties like Cancel, Uri, etc.

You can use the Cancel property of the NavigatingCancelEventArgs to cancel the navigation.

Using the code

Below is a sample source code demonstrating how to cancel the navigation in Windows Phone programmatically.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneApp6
    public partial class MainPage : PhoneApplicationPage
        // Constructor
        public MainPage()
        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
            if (MessageBox.Show("You are about to Navigate to a Different Page . " + 
                "Do you want to continue ?", "Confirmation", 
                MessageBoxButton.OKCancel)== MessageBoxResult.Cancel)
                e.Cancel = true;
        private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
        private void button1_Click(object sender, RoutedEventArgs e)
            NavigationService.Source = new Uri("/Page1.xaml", UriKind.Relative);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
Senthil Kumar is a Software Engineer who has around 3 years of experience in IT industry. He is currently working as a Software Engineer in Bangalore and works mainly on the Windows or Client Development technologies and has good working experience in C#/.NET, Delphi, Winforms and SQL Server.

He is also a Microsoft Technology Certified Professional in ASP.NET.

Senthil completed his Master of Computer Applications from Christ College (Autonomous), Bangalore in the year 2009 and is a MCA Rank Holder. He has passion for Microsoft technologies especially Windows Phone development.

You may also be interested in...


Comments and Discussions

QuestionCustommessage box Pin
mdutta22-Apr-14 15:28
membermdutta22-Apr-14 15:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 5 Oct 2012
Article Copyright 2012 by isenthil
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid