2010-12-06 123 views
1

我在我的WP7應用程序中有一個ErrorPage.xaml頁面。當發生未處理的異常時,將調用app.xaml.cs中的方法,並將異常傳遞給ErrorPage.xaml.cs,並通過鏈接將錯誤顯示給用戶,以返回主頁面。
如果我點擊AppBar IconButton的速度太快,導致失敗事件被引發,errorpage仍然被調用,但沒有任何內容顯示在錯誤頁面上。 AppBar是唯一可見的東西。wp7錯誤處理

想不通爲什麼

這裏是我的app.xaml.cs代碼

 // Code to execute on Unhandled Exceptions 
    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) 
    { 
     if (System.Diagnostics.Debugger.IsAttached) 
     { 
      // An unhandled exception has occurred; break into the debugger 
      System.Diagnostics.Debugger.Break(); 
     } 

     e.Handled = true; 
     ErrorPage.Exception = e.ExceptionObject; 
     (RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source = 
      new Uri(Navigation.PAGE_ERROR, UriKind.Relative); 
    } 

Error.xaml是這樣

<Grid x:Name="LayoutRoot" Background="{StaticResource GlobalPageBackgroundBrush}" CacheMode="BitmapCache"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12"> 
     <TextBlock x:Name="ApplicationTitle" Text="{StaticResource AppName}" Style="{StaticResource PhoneTextNormalStyle}" FontWeight="SemiBold"/> 
     <TextBlock x:Name="PageTitle" Text="error" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 
    <!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1"> 
     <TextBlock x:Name="ErrorText" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap" /> 
     <StackPanel x:Name="FriendlyErrorStackPanel" Margin="0,100,0,0" Orientation="Vertical"> 
      <TextBlock Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"> 
       <TextBlock.Text> 
        Please click the link below to return to the main page of the application. 
       </TextBlock.Text> 
      </TextBlock>     
      <HyperlinkButton Style="{StaticResource PhoneHyperlinkStyle}" Content="Start Over" NavigateUri="/Views/MainPage.xaml" /> 
     </StackPanel> 
    </Grid> 
</Grid> 

,最後的errorPage。 xaml.cs是

public partial class ErrorPage : PhoneApplicationPage 
{ 
    public ErrorPage() 
    { 
     InitializeComponent(); 
    } 

    public static Exception Exception; 

    // Executes when the user navigates to this page. 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     if (Exception != null) 
     { 
      if (System.Diagnostics.Debugger.IsAttached) 
      { 
       FriendlyErrorStackPanel.Visibility = System.Windows.Visibility.Collapsed; 
       ErrorText.Text = Exception.ToString(); 
      } 
      else 
      { 
       FriendlyErrorStackPanel.Visibility = System.Windows.Visibility.Visible; 
       ErrorText.Text = GlobalConstants.DEFAULT_ERROR_MESSAGE; 
      } 
     } 

     base.OnNavigatedTo(e); 
    } 
} 
+0

我擔心的是,您正在從這個錯誤頁面導航**正向**回主。這意味着你可以按下_back_ 2x,並且可能回到錯誤狀態? – 2010-12-07 19:08:10

回答

0

我懷疑一旦它遇到Application_UnhandledException並且Exception被設置爲Handled = true,如果你快速連續得到兩個,它可能會變得脆弱 - 解決這個問題的方法是確保AppBar按鈕在嘗試時只允許一次按下做一些事情(所以在試圖導航時禁用,如果失敗則重新啓用)

此外,如果您尚未這樣做,請考慮對Navigate方法調用使用Dispatcher.BeginInvoke()。