WPF页面导航NullReferenceException

时间:2011-09-10 14:15:48

标签: c# wpf xaml

我正在测试项目中,我有一个带有框架和两个按钮的mainWindow.Xaml:

<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>

守则背后是:

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));
}

如此简单。 Evrey按钮在框架上打开一个页面。 问题在于此代码:

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;
        }
    }

当我点击第1页的openong按钮时,我想要一条警告信息,如果我点击“否”,我会留在这个页面,否则,我继续第2页。但是当我尝试进入第2页时,我有一个NullReferenceException on CancelNavigationPage_Unloaded void。 有人可以解释我如何解决这个问题吗?

提前致谢

编辑: 我用这种方式修改了:

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);
            }
        }

通过这种方式,如果我点击按钮2它可以正常工作,如果我点击button1,它在同一个Page1,应用程序不会问我是否要离开页面。

1 个答案:

答案 0 :(得分:3)

当您尝试删除事件处理程序时,您的NavigationService在已卸载事件中已为空,这就是您收到错误的原因。

尝试将Navigating EventHandler中的条件更改为以下内容:

        // 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);
        }
    }