(转自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)的更多相关文章

  1. Don't let self-built concept imprison yourself

    If Self-inferiority is disease, but self-confidence is hazard. Leo moon personalities can be extreme ...

  2. New XAMPP security concept:错误解决方法

    New XAMPP security concept:错误解决方法 (2014-03-06 16:07:46) 转载▼   分类: php 在Linux上配置xampp后远程访问域名报错: New X ...

  3. 【译文】 C#面向对象的基本概念 (Basic C# OOP Concept) 第一部分(类,对象,变量,方法,访问修饰符)

    译文出处:http://www.codeproject.com/Articles/838365/Basic-Csharp-OOP-Concept 相关文档:http://files.cnblogs.c ...

  4. xampp 访问出现New XAMPP security concept 解决办法

    最近通过手机访问本地服务器时出现以下问题: Access forbidden! New XAMPP security concept: Access to the requested director ...

  5. xampp 访问出现New XAMPP security concept

    在浏览器输入 http://60.10.140.22/xampp出现以下错误信息: Access forbidden! New XAMPP security concept: Access to th ...

  6. the basic index concept

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Over the years numerous variations o ...

  7. Notes of Linked Data concept and application - TODO

    Motivation [反正债多了不愁,再开个方向.] Data plays a core role in most business systems, data storage and retrie ...

  8. 【转】Basic C# OOP Concept

    This Article will explain a very simple way to understand the basic C# OOP Concept Download ShanuBas ...

  9. [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 ...

随机推荐

  1. 九度oj 题目1466:排列与二进制

    题目描述: 在组合数学中,我们学过排列数.从n个不同元素中取出m(m<=n)个元素的所有排列的个数,叫做从n中取m的排列数,记为p(n, m).具体计算方法为p(n, m)=n(n-1)(n-2 ...

  2. 实现chrome多用户独立cookie

    2018-02-08 10:58:57 在浏览器设置中添加一个用户并创建桌面快捷方式,属性中我们可以发现 "C:\Program Files (x86)\Google\Chrome\Appl ...

  3. HDU-2234 无题I

    为每个状态定义两个函数S和H,分别表示当前状态到列一致和行一致的目标状态的最少操作次数. 然后有了估价函数F=Min(S,H)就可以IDA*了. #include <cstdio> #in ...

  4. jquery工具方法总结

    $.extend 对象合并,支持深拷贝 $.each 相当于array.each或object.each,可以遍历数组和对象 $.grep 相当于array.filter $.map 相当于array ...

  5. Java面试题之ArrayList和LinkedList的区别

    先看下类图: 相同点: 都实现了List接口和Collection: 不同点: 1.ArrayList是基于数组实现的:LinkedList是基于链表实现的: 2.ArrayList随机查询速度快:L ...

  6. spring-boot项目热部署以及spring-devtools导致同类不能转换

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  7. 致命错误:ext/standard/php_smart_str.h:没有那个文件或目录

    致命错误:ext/standard/php_smart_str.h:没有那个文件或目录 参考文章:https://blog.csdn.net/jartins/article/details/80371 ...

  8. AC日记——旅行 洛谷 P3313

    题目描述 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我 ...

  9. python 当pip不能用的时候可以去找python安装包

    初学python,一直pip安装各种包,突然间有一天pip莫名其妙不能用了,除了pip help全部都是没反应 百度好像没人出现过pip挂掉的情况 花了一小时修复pip,卸载啊,路径啊,全部无效 百度 ...

  10. Codeforces 553D Nudist Beach(二分答案 + BFS)

    题目链接 Nudist Beach 来源  Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...