wp7错误处理

时间:2010-12-06 23:29:27

标签: error-handling windows-phone-7

我的WP7应用程序中有一个ErrorPage.xaml页面。当发生未处理的异常时,将调用app.xaml.cs中的方法,并将异常传递给ErrorPage.xaml.cs,并向用户显示错误,并返回返回主页面的链接。
如果我太快地单击AppBar IconButton,则会引发Navigation Failed事件,仍会调用errorpage,但错误页面上不会显示任何内容。 AppBar是唯一可见的东西。

无法弄清楚原因

以下是我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);
    }
}

1 个答案:

答案 0 :(得分:0)

我怀疑一旦它遇到Application_UnhandledException并且Exception被设置为Handled = true,如果快速连续获得两个,它可能会变得不稳定 - 这样做的方法是确保AppBar Button只允许一次按下尝试做某事(所以在尝试导航时禁用,如果失败则重新启用)

另外,如果您还没有使用Dispatcher.BeginInvoke()进行Navigate方法调用,请考虑使用它。