WPF02(concept)
(转自http://www.cnblogs.com/huangxincheng/archive/2012/06/17/2552322.html)这些天从项目上接触到了wpf,感觉有必要做一个笔记,首篇还是聊聊基本的概念,要学习wpf,我们需要采用webform的思维来考虑问题。
一:App环境承载
我们都知道,console和winform程序的入口函数都是main,wpf同样也不例外,好了,我们新建一个wpf的程序,vs自动给我们生成了一个
MainWindow.xaml和App.xaml文件。
微软官方说wpf程序是从Application开始的,既然是开始总有个入口点吧,奇怪的是我们并没有发现Main函数,程序又是如何Run起来的呢?
其实,wpf为了简化我们的工作,把一些机械性的代码透明了,那么我们如何找到这个Main函数呢?很简单,我们编译一下程序,发现
App.xaml最后生成了App.g.cs的部分类,并且发现StartupUri是MainWindow.xaml,也就是说程序一运行,MainWindow.xaml将会启动。
二:Wpf中Application的生命周期
我们知道webform中的Global文件定义了一个应用程序的全局生命周期,或许有人问,生命周期能够干些什么,其实干的事情可多着呢,
比如我们可以做一些身份验证,或者一些信息的初始化,那么wpf中到底有哪些对应的方法和事件呢?
1:OnStartup方法 => Startup 事件
这个就见名识意了,也就是上面一幅图中的app.Run()的时候触发。
2: OnSessionEnding方法 => SessionEnding 事件
系统关机前调用。
3:OnExit方法 => Exit事件
应用程序关闭前调用。
4:OnActivated方法 => Activated 事件
应用程序获得焦点的时候触发。
5:OnDeactivated方法 => DeActivated事件
应用程序失去焦点的时候触发。

1 using System;
2 using System.Collections.Generic;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Windows;
7
8 namespace Demo
9 {
10 /// <summary>
11 /// App.xaml 的交互逻辑
12 /// </summary>
13 public partial class App : Application
14 {
15 protected override void OnActivated(EventArgs e)
16 {
17 base.OnActivated(e);
18
19 //TODO your code
20 }
21
22 protected override void OnDeactivated(EventArgs e)
23 {
24 base.OnDeactivated(e);
25
26 //TODO your code
27 }
28
29 protected override void OnExit(ExitEventArgs e)
30 {
31 base.OnExit(e);
32
33 //TODO your code
34 }
35
36 protected override void OnStartup(StartupEventArgs e)
37 {
38 base.OnStartup(e);
39
40 //TODO your code
41 }
42
43 protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
44 {
45 base.OnSessionEnding(e);
46
47 //TODO your code
48 }
49 }
50 }

三:全局异常获取
在webform中的Global文件中有一个Application_Error方法,专门用来捕获整个应用程序的异常,以至于不会出现“黄白页”给用户,以此来提高
系统的健壮性和安全性,那么wpf中也有类似的方法吗?当然,wpf跟webform神似,他有的我也有,这里是一个DispatcherUnhandledException
事件,然后我们在OnStartup注册一下就Ok了。

1 namespace Demo
2 {
3 /// <summary>
4 /// App.xaml 的交互逻辑
5 /// </summary>
6 public partial class App : Application
7 {
8 protected override void OnStartup(StartupEventArgs e)
9 {
10 base.OnStartup(e);
11
12 //注册Application_Error
13 this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
14
15 }
16
17 //异常处理逻辑
18 void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
19 {
20 //处理完后,我们需要将Handler=true表示已此异常已处理过
21 e.Handled = true;
22 }
23 }
24 }

好,下面我们做了示例:
首先我们拖一个button,事件处理中故意抛出异常。

1 namespace Demo
2 {
3 /// <summary>
4 /// MainWindow.xaml 的交互逻辑
5 /// </summary>
6 public partial class MainWindow : Window
7 {
8 public MainWindow()
9 {
10 InitializeComponent();
11 }
12
13 private void button1_Click(object sender, RoutedEventArgs e)
14 {
15 throw new Exception("我就害你,我就抛异常");
16 }
17 }
18 }

然后我们在Application_Error中进行处理,当然实际应用中应该是记一些log日志。

1 //异常处理逻辑
2 void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
3 {
4 MessageBox.Show("谁tmd惹祸了:" + e.Exception.Message);
5
6 //处理完后,我们需要将Handler=true表示已此异常已处理过
7 e.Handled = true;
8 }

最后看一下效果,注意,我们的程序并没有崩溃。
WPF02(concept)的更多相关文章
- Don't let self-built concept imprison yourself
If Self-inferiority is disease, but self-confidence is hazard. Leo moon personalities can be extreme ...
- New XAMPP security concept:错误解决方法
New XAMPP security concept:错误解决方法 (2014-03-06 16:07:46) 转载▼ 分类: php 在Linux上配置xampp后远程访问域名报错: New X ...
- 【译文】 C#面向对象的基本概念 (Basic C# OOP Concept) 第一部分(类,对象,变量,方法,访问修饰符)
译文出处:http://www.codeproject.com/Articles/838365/Basic-Csharp-OOP-Concept 相关文档:http://files.cnblogs.c ...
- xampp 访问出现New XAMPP security concept 解决办法
最近通过手机访问本地服务器时出现以下问题: Access forbidden! New XAMPP security concept: Access to the requested director ...
- xampp 访问出现New XAMPP security concept
在浏览器输入 http://60.10.140.22/xampp出现以下错误信息: Access forbidden! New XAMPP security concept: Access to th ...
- the basic index concept
Computer Science An Overview _J. Glenn Brookshear _11th Edition Over the years numerous variations o ...
- Notes of Linked Data concept and application - TODO
Motivation [反正债多了不愁,再开个方向.] Data plays a core role in most business systems, data storage and retrie ...
- 【转】Basic C# OOP Concept
This Article will explain a very simple way to understand the basic C# OOP Concept Download ShanuBas ...
- [Angular2 Router] Configuring a Home Route and Fallback Route - Learn An Essential Routing Concept
In this tutorial we are going to learn how to configure the Angular 2 router to cover some commonly ...
随机推荐
- 九度oj 题目1374:所有员工年龄排序
题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你能写出这样的程序吗? 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为一 ...
- Event based Collections
https://sourceforge.net/p/happy-guys/wiki/Event%20based%20Collections/
- Luogu【P1880】石子合并(环形DP)
先放上luogu的石子合并题目链接 这是一道环形DP题,思想和能量项链很像,在预处理过程中的手法跟乘积最大相像. 用一个m[][]数组来存储石子数量,m[i][j]表示从第 i 堆石子到第 j 堆石子 ...
- C语言扩展题
1.使用cmake来创建c语言工程 2.使用gcc来编译源代码 3.下载redis,并且编译运行redis(注:redis目前是c语言编写的,而且是主要是linux平台,在windows平台编译比较麻 ...
- jsp文件放在WebRoot下还是WebInfo下
观点一:(较为赞同) 安全性不是真正的原因,因为jsp是要解析后才显示到浏览器的,即使用户知道你jsp的路径,也不可能通过浏览器看到jsp源码的,而如果是通过其它手段入侵服务器的话,放在WEB-INF ...
- 【Visual Studio】让用VS2012/VS2013编写的程序在XP中顺利运行(转)
原文转自 http://blog.csdn.net/asanscape/article/details/38752655 微软为了推销自家平台,默认配置下VS2012和VS2013编写的应用程序只能在 ...
- 内核的bootmem内存分配器【转】
转自:http://blog.csdn.net/zmxiangde_88/article/details/8041040 版权声明:本文为博主原创文章,未经博主允许不得转载. 在内核启动期间,伙伴系统 ...
- Python和其他进程的管道通信方式--popen和popen2的比较
目前有一个查询程序 get_user_id 是用C写的,python需要调用这个程序:使用 get_user_id "用户名" 可以得到输出: "ID0002451&q ...
- PhPStorm 快捷键使用(转载)
PhPStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具,PhpStorm可随时帮助用户对其编码进行调整,运行单元测试或者提供可视化debug功能.Phpstrom的一款名 ...
- 设置USB数据监听
设置USB数据监听 在Kali Linux中,USB也是作为一个通信端口进行存在.常见的鼠标.键盘.U盘都是通过USB接口传输数据.所以,对于USB接口也可以实施监听,类似网络接口一样.在进行US ...