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 ...
随机推荐
- 在springmvc中使用@PathVariable时,应该注意点什么?
导读:近来在做库存调剂系统时,我从前台到后台的传值方式,主要包括:1个,用@PathVariable或者@RequestParam从路径取:大于一个,用于更新或者添加操作的,我用的是表单实体传到后台: ...
- CentOS7下RabbitMQ服务安装配置胜多负少
RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发.RabbitMQ据说具有良 ...
- JSP内置对象的cookie和session实现简单登录界面
创建一个index.jsp页面 <%@ page language="java" contentType="text/html; charset=utf-8&quo ...
- 多元线性回归(pandas/scikit-learn)
import pandas as pd from sklearn.cross_validation import train_test_split from sklearn.linear_model ...
- 【Bzoj3944】杜教筛模板(狄利克雷卷积搞杜教筛)
题目链接 哇杜教筛超炫的 有没有见过$O(n^\frac{2}{3})$求欧拉函数前缀和的算法?没有吧?蛤蛤蛤 首先我们来看狄利克雷卷积是什么 首先我们把定义域是整数,陪域是复数的函数叫做数论函数. ...
- BZOJ 4584 [Apio2016]赛艇 ——动态规划
Subtask 1 直接$N^2$ $DP$,就可以了 Subtask 2 用$f[i][j]$表示当前位置为$i$,结束元素为$j$的方案数. Subtask 3 看下面 Subtask 4 首先可 ...
- 给长标题加...css
.wrap{ white-space:nowrap;overflow:hidden;text-overflow: ellipsis; } <th class="wrap"&g ...
- Java连接SQLite数据库
下载java包:sqlite-jdbc-3.7.2.jar,放到java工程目录lib下 如下代码实例: import java.sql.*; import org.sqlite.JDBC; /** ...
- word文档的导出(用freemarker模板导出)(桃)
1.将要导出的word文档另存为xml格式的 2.用文档编辑器打开(如:notepad++),将要展示的数据用${name}的形式替换,“name”对应数据库中的字段 3.根据模板生成 package ...
- scss 侧边栏_图片
html <!doctype html><html lang="zh-CN"><head> <meta charset="UTF ...