2011-09-10 108 views
0

我在一個測試項目是其中一個I'have與mainWindow.Xaml框和兩個按鈕:WPF與頁面導航的NullReferenceException

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

當我點擊鏈接openong的第一頁,我想警告消息,如果我點擊「否」我留在這個頁面,否則,我去page2.But當我嘗試去第2頁時,我在CancelNavigationPage_Unloaded void上有一個NullReferenceException。 有人可以解釋我如何解決這個問題嗎?

在此先感謝

編輯: 我這樣修改:

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它工作正常點擊,如果我按一下按鈕1,也就是在相同的Page1,應用程序does'nt問我是否要離開頁面。

+0

堆棧跟蹤,實際的錯誤行? –

+0

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

回答

3

當您試圖刪除您的事件處理程序時,您的NavigationService在Unloaded Event中已爲空,這就是您遇到錯誤的原因。

試着改變你的導航事件處理程序的條件,以這樣的:

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

謝謝。有效。 – AlessandroG