Windows Phone学习笔记:在应用程序中处理错误
来源:互联网 发布:淘宝买家要发票怎么办 编辑:程序博客网 时间:2024/04/30 08:14
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
1. 首先,添加一个新的页面到项目工程中。在Solution Explorer视图,右键单击WindowsPhonePuzzle工程节点, 指向Add并选择New Item. 在Add New Item对话框, 从模板列表里选择Windows Phone Portrait Page,把name 设为ErrorPage.xaml然后点击Add。
为工程添加一个新的页面
2. 在ErrorPage.xaml文件中,找到名为LayoutRoot的Grid元素,并用下面的蓝色高亮XAML标记代码替换它的子控件。这个XAML定义了一个应用程序标题和页面标题,两个标题都被命名为error。它同样定义了一个TextBlock对象并被指定为x:Name="ErrorText"样式,用来保存任何未来异常的错误文本。
XAML
...
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and pagetitle-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
<TextBlock x:Name="ApplicationTitle" Text="WINDOWS PHONE PUZZLE" Style="{StaticResource PhoneTextNormalStyle}"/>
<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">
<Border BorderBrush="White">
<TextBlock x:Name="ErrorText" Style="{StaticResource PhoneTextSmallStyle}"TextWrapping="Wrap" />
</Border>
</Grid>
</Grid>
...
3. 现在,按下F7来打开一个新页面的代码隐藏文件。或者,作为另一种选择,在Solution Explorer视窗上右键单击ErrorPage.xaml然后选择View Code。在文件的最上层插入下面的命名空间。
C#
usingSystem.Windows.Navigation;
4. 然后,把下面代码段中高亮的部分插入到ErrorPag类中。这样做就建立了一个Exception对象,当跳转切换到这个页面的时候会自动挂接到ErrorText.Text。
C#
publicpartialclassErrorPage :PhoneApplicationPage
{
public ErrorPage()
{
InitializeComponent();
}
publicstaticException Exception;
// Executes when the user navigates to thispage.
protectedoverridevoid OnNavigatedTo(NavigationEventArgse)
{
ErrorText.Text =Exception.ToString();
}
}
5. 当这个页面完成后,无论何时,当一个不能被处理的异常出现,此类事件的句柄会使程序切换到错误的页面,并显示错误的信息。在Solution Explorer视图上,右键单击App.xaml并选择View Code来打开Application类的代码隐藏类。
C#
//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 inthe debugger
System.Diagnostics.Debugger.Break();
}
e.Handled =true;
ErrorPage.Exception= e.ExceptionObject;
(RootVisualas Microsoft.Phone.Controls.PhoneApplicationFrame).Source=newUri("/ErrorPage.xaml",UriKind.Relative);
}
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
- Windows Phone学习笔记:在应用程序中处理错误
- 如何在 Windows Phone 的代码中创建应用程序栏
- 在Windows Phone Emulator中构建并测试应用程序
- 如何在 Windows Phone 应用程序的一个 Pivot 控件中使用不同的应用程序栏
- Windows phone 8 学习笔记
- Windows Phone应用程序中加密解密数据
- Windows Phone 学习 在程序中调用直接拨打电话
- Windows Phone 学习 直接在程序中发电子邮件
- 在windows phone应用程序中自定义全局类保存应用信息
- 在windows phone应用程序中使用友盟统计应用信息
- windows phone:在Windows Phone中如何显示GIF图片
- windows phone:应用程序状态
- windows phone应用程序生命周期
- Android学习笔记----在一个应用程序中启动另一个应用程序
- 笔记(一):第一个windows phone应用程序
- 学习笔记之---windows phone平台概况
- Windows Phone 学习笔记(1)-环境搭建
- Windows Phone学习笔记:创建用户界面
- 《多核程序设计》Part 6:OpenMP 一种可移植的多线程解决方案(2)
- android-使用ViewGroup实现左右滑动切换界面以及界面裂开的效果
- Paragon NTFS卸载之后移动硬盘不能用的解决方法
- jQuery 入门教程(16): 设置或取得元素的CSS class
- 完整的DirectShow捕获音视频的demo(源码)
- Windows Phone学习笔记:在应用程序中处理错误
- ajax心得1--ajax入门介绍
- ajax心得2--ajax主要对象、方法以及属性的介绍
- ajax心得3--编写ajax同用工具代码以及案例分析
- 地面网络传输层之TCP、UDP详解
- 用图片隐藏信息的技术实现
- S3C2440驱动之按键驱动(一)
- Android---把数据保存到数据库中(二)
- NtQueryInformationProcess用法