AlessandroG
AlessandroG

Reputation: 318

WPF with page navigation NullReferenceException

I'm in a test project where I'have one mainWindow.Xaml with a frame and two buttons:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>

        <Button Name="button1" Click="button1_Click">button1</Button>
        <Button Name="button2" Click="button2_Click">button2</Button>
        <Frame Name="frame"></Frame>
    </StackPanel>
</Window>

the Code Behind is this:

private void button1_Click(object sender, RoutedEventArgs e)
{
    frame.Navigate(new Uri("Page1.xaml", UriKind.Relative));
}

private void button2_Click(object sender, RoutedEventArgs e)
{
    frame.Navigate(new Uri("Page2.xaml", UriKind.Relative));
}

So simply. Evrey button open a page on the frame. The problem is in this code:

public partial class Page1 : Page
    {
        public Page1()
        {
            this.Loaded += new RoutedEventHandler(CancelNavigationPage_Loaded);
            this.Unloaded += new RoutedEventHandler(CancelNavigationPage_Unloaded);
            InitializeComponent();
        }
        void CancelNavigationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);
        }

        void CancelNavigationPage_Unloaded(object sender, RoutedEventArgs e)
        {


            this.NavigationService.Navigating -= new NavigatingCancelEventHandler(NavigationService_Navigating);

        }

        void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            // Does the user really want to navigate to another page?
            MessageBoxResult result;
            result = MessageBox.Show("Do you want to leave this page?", "Navigation Request", MessageBoxButton.YesNo);

            // If the user doesn't want to navigate away, cancel the navigation
            if (result == MessageBoxResult.No) e.Cancel = true;
        }
    }

When I click on button openong the page1, I want an alert message, and if I click "No" I remain in this page otherwise, I go on page2.But when I try to go in page2 I have a NullReferenceException on CancelNavigationPage_Unloaded void. Can someone explain me how to solve this?

Thanks in advance

EDIT: I modified in this way:

void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (this.NavigationService.CurrentSource==this.NavigationService.Source)
            {

                this.NavigationService.StopLoading();
                return;
            }
            // Does the user really want to navigate to another page?
            MessageBoxResult result;
            result = MessageBox.Show("Do you want to leave this page?", "Navigation Request", MessageBoxButton.YesNo);

            // If the user doesn't want to navigate away, cancel the navigation 
            if (result == MessageBoxResult.No)
                e.Cancel = true;
            else  // Remove Handler
            {
                if (this.NavigationService != null)
                    this.NavigationService.Navigating -= new NavigatingCancelEventHandler(NavigationService_Navigating);
            }
        }

In this way if I click in button 2 it works correctly, and if I click on button1, that is in the same Page1, the application does'nt ask me if I want to leave the page.

Upvotes: 0

Views: 3698

Answers (1)

Mark Hall
Mark Hall

Reputation: 54542

Your NavigationService is already null in the Unloaded Event when you are trying to remove your eventhandler that is why you are getting the error.

Try changing the conditional in your Navigating EventHandler to something like this:

        // If the user doesn't want to navigate away, cancel the navigation 
        if (result == MessageBoxResult.No) 
            e.Cancel = true;
        else  // Remove Handler
        {
            if (this.NavigationService != null)
                this.NavigationService.Navigating -= new NavigatingCancelEventHandler(NavigationService_Navigating);
        }
    }

Upvotes: 3

Related Questions