WPF Application
Application类作为启动的入口,在VS中,通常自动代码为我们继承了Application类,这样做的有点,我还没有理解到,但是我们先学到这个知识点。
为了能够更好的控制整个启动过程,包括得到Active,LoadComplete,Deactive,SessionEnding等事件,可以主动改变程序的入口,即是static void Main(string[] args)方法的所在位置。
我们主动添加一个Program.cs(当然叫别的名字是可以的)的文件,添加Main方法,并在其中修改启动,代码就可以了。说的比较粗略,看代码吧。
Program.cs
using System; namespace LearnWPF
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
App app = new App();
myWInd m = new myWInd();
app.MainWindow = m;
m.Show();
app.Run();
}
}
}
App.xaml.cs
using System;
using System.Diagnostics;
using System.Windows; namespace LearnWPF
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
Debug.WriteLine("OnActived");
} protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
Debug.WriteLine("OnDeactived");
} protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
MessageBox.Show("Exiting");
} protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);
MessageBox.Show("you're quitting");
} protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MessageBox.Show("Staring");
} protected override void OnLoadCompleted(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnLoadCompleted(e);
MessageBox.Show("completed");
}
}
}
App.xaml 这文件指明了启动的窗口等内容,所以需要相应地修改。
<!--application 的 class属性可以随便写-->
<!--application 的 StartupUri属性指明了 启动的窗体-->
<Application x:Class="muhaha"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources> </Application.Resources>
</Application>
但是,到了这里还是不行的;因为对于App.xaml在自动生成的代码里,会有Main方法入口,因此还需要改变程序的入口,在项目的属性里修改配置。请注意,刚刚添加Program.cs文件后,可能这里不会有,编译一次,就会在这里出现了,如下图所示。

希望能够帮助一些同学。
WPF Application的更多相关文章
- Handling events in an MVVM WPF application
Posted: June 30, 2013 | Filed under: MVVM, WPF, XAML |1 Comment In a WPF application that uses the ...
- 【转】How to view word document in WPF application
How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...
- Merging a WPF application into a single EXE(WPF应用程序合并成单个Exe文件)
I always dislike handing off little applications to people. Not because I can’t, but because of the ...
- C# WPF Application 下的文件操作
好气哦,电脑好烂,每天花大把的时间在等电脑反应上. 没有钱买新电脑,连组台式机的钱都没有.好气哦. 啊啊啊啊文件操作是什么鬼???C++下我都懵了,C#下好多东西要学!!!我不会!我不会!我不会!!! ...
- 每天翻译一点点: WPF Application Framework (WAF)
ps:http://waf.codeplex.com/wikipage?title=Model-View-ViewModel%20Pattern&referringTitle=Document ...
- WPF Application 类介绍以及怎样修改启动方式
因为想要修改wpf的启动方式,所以研究了下Application类,现把一些有用的属性与大家分享下: 属性: Current 获取当前 AppDomain的 Appl ...
- WPF——Application
Application类处于WPF应用程序的最顶端,main函数就在这个类中. Application类的作用: 截图连接 https://docs.microsoft.com/zh-cn/dotne ...
- vs 编译错误 The name 'InitializeComponent' does not exist in the current context in WPF application
1:文件命名空间的问题 xaml文件和model.cs文件的命名空间 2:csproj 那么它究竟是给谁用的呢?那是给开发工具用的,例如我们在熟悉不过的Visual Studio,以及大家可以没有接触 ...
- WPF Application 执行顺序
public static void Main() { ApplicationClass.App app = new ApplicationClass.App();app.InitializeComp ...
随机推荐
- Robots惊恐记
昨天发现在百度上搜索不到网站krely.cn的关键词(季小鱼),我记得之前的排名是第四位.到底是哪里的错误导致这个问题呢. 百度排名丢失,那么360会不会也出现同样的错误呢. 可以看到,360提示是我 ...
- Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'
The error that occurs is:Fatal error: Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY' in /w ...
- [转]Avoiding GDB Signal Noise.
原文:http://peeterjoot.wordpress.com/2010/07/07/avoiding-gdb-signal-noise/ A quick note for future ref ...
- (Sql Server)数据的拆分和合并
(Sql Server)数据的拆分和合并 背景: 今天遇到了数据合并和拆分的问题,尝试了几种写法.但大致可分为两类:一.原始写法.二.Sql Server 2005之后支持的写法.第一种写法复杂而且效 ...
- Fortran使用隐形DO循环和reshape给一维和多维数组赋初值
Fortran可以使用隐形DO循环和reshape给一维和多维数组赋初值. 下面以一维数组和二维数组为例,并给出程序结果: program main implicit none integer::i, ...
- 定时器Timer不定时
订餐系统之定时器Timer不定时 经过几天漫长的问题分析.处理.测试.验证,定时器Timer终于定时了,于是开始了这篇文章,希望对还在纠结于“定时器Timer不定时”的同学有所帮助,现在的方案,在系统 ...
- php5.3.*编译出现make: *** [ext/gd/libgd/gd_compat.lo] Error 1 解决方法
升级系统,把php5.2.9升级到5.3.6,按照以前的编译参数,configure能正常通过,但是在make的时候提示: In file included from /root/php-5.3.6 ...
- 使用WCF Data Service 创建OData服务
使用WCF Data Service 创建OData服务 在 上一章 中,介绍了如何通过 OData 协议来访问 OData 服务提供的资源.下面来介绍如何创建一个 OData 服务.在这篇文章中,主 ...
- A*算法&博弈树α-β剪枝
A*算法&博弈树α-β剪枝 A*算法/博弈树 前阵子考试学了A*算法.博弈树和回溯,自己真是愚蠢至极,根本没就搞明白这些,所以对于这些算法问道的话就不能说清楚,也记不住,所以才有了这篇笔记.在 ...
- 黑马程序员:Java基础总结----java注解
黑马程序员:Java基础总结 java注解 ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! java注解 lang包中的基本注解 @SuppressWarnings ...