I have an ErrorPage.xaml page in my WP7 application. When an unhandled exception occurs, the method in app.xaml.cs is called and that passes the exception to the ErrorPage.xaml.cs and the error is displayed to the user with a link to go back to the main page.
If I click on the AppBar IconButton too fast, then the Navigation Failed event is raised, errorpage is still called but nothing is displayed on the error page. The AppBar is the only thing visible.
Cannot figure out why
Here is the code in my 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 is like this
<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>
and finally the ErrorPage.xaml.cs is
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);
}
}
I suspect once it hits Application_UnhandledException and the Exception is set as Handled = true, it might get flaky if you get two in rapid succession – the way around this would be to make sure that the AppBar Button allows only one press while its trying to do something (so disable while its trying to navigate, and re-enable if it fails)
Also, consider using Dispatcher.BeginInvoke() for your Navigate method call if you aren’t already doing so.