gpt4 book ai didi

error-handling - wp7错误处理

转载 作者:行者123 更新时间:2023-12-03 08:02:19 25 4
gpt4 key购买 nike

我的WP7应用程序中有一个ErrorPage.xaml页面。发生未处理的异常时,将调用app.xaml.cs中的方法,并将该异常传递给ErrorPage.xaml.cs,并通过链接返回到用户的方式向用户显示错误。
如果我单击AppBar IconButton的速度过快,则会引发“导航失败”事件,仍然会调用错误页面,但错误页面上不会显示任何内容。 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);
}
}

最佳答案

我怀疑一旦击中Application_UnhandledException并将Exception设置为Handled = true,则如果快速连续获得两次,它可能会变得片状-解决方法是确保AppBar Button在尝试执行操作时仅允许按一下某些东西(因此在尝试导航时将其禁用,如果失败则重新启用)

另外,请考虑对您的Navigate方法调用使用Dispatcher.BeginInvoke()(如果尚未这样做)。

关于error-handling - wp7错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4372037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com