首页 技术 正文
技术 2022年11月11日
0 收藏 874 点赞 2,450 浏览 2456 个字

wpf应用程序在启动的时候会自动创建Main函数并调用Application实例的run(),从而启动Application进程。Main函数在一个App.g.cs文件中,App.g.cs文件的位置在\obj\x86\Debug\App.g.cs。自动生成的Main函数如下:

        /// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main() {
WpfApp1031.App app = new WpfApp1031.App();
app.InitializeComponent();
app.Run();
}

如果我们想在Main函数中获取参数或做一些用户验证,就可以在此修改Main函数,但是你会发现项目关闭再重新启动的时候,App.g.cs会重新生成,所以我们必须放弃在App.g.cs文件中修改Main函数。解决方案如下:

第一步:打开App.xaml文件的“属性”窗口,修改生成操作:ApplicationDefinition 为Page。

【WPF】Application应用程序启动

第一步:我们可以在App.xaml.cs中重新写Main函数,启动主窗口的方法有三种,方法1比较常见。代码如下:

public partial class App : Application
{
[STAThread]
static void Main(string[] args)
{
//用户验证操作Start
//......
//用户验证操作Start //方法1
Application app = new Application();
Window1 win1 = new Window1();
app.Run(win1); //方法2
//Application app = new Application();
//Window1 win1 = new Window1();
//app.MainWindow = win1;
//win1.Show();
//app.Run(); //方法3
//Application app = new Application();
//app.StartupUri = new Uri("Window1.xaml", UriKind.Relative);
//app.Run();
}
}

第三步:修改项目的启动对象为App类,默认(未设置)

【WPF】Application应用程序启动

重新生成就可以了。

  各个Buid Action之间的区别 (生成操作的各个选项目前还不太明白,等以后慢慢了解吧!)

  * None: 此文件不参与编译也不被输出。比如:工程中的文档文件, readme.txt。

  * Compile: 参与编译并输出。主要是代码文件。

  * Content: 不参与编译,但会被输出。

  * Embedded Resource: 此文件被嵌入到主工程生成的DLL或exe中。主要是资源文件。

  * ApplicationDefinition: 和Page类似,但只用于Silverlight的启动页面(默认是App.xaml)。

  * Page: Silverligh中所有的usercontrol/page/childwindow xaml都属于”Page” build,其它的build action不能将code behind文件和xaml文件连接起来。

  * CodeAnalysisDictionary: 自定义的CodeAnalysis字典。

  * Resource:embeds the file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName.g.resources

  * SplashScreen: Silverlight的欢迎界面。

  * DesignData: Sample data types will be created as faux types. Use this Build Action when the sample data types are not creatable or have read-only properties that you want to defined sample data values for.

  * DesignDataWithDesignTimeCreatableTypes: Sample data types will be created using the types defined in the sample data file. Use this Build Action when the sample data types are creatable using their default empty constructor.

  * EntityDeploy: 适用于Entity框架。

经过尝试,我们也可是自己重新创建一个program类来写Main函数,然后修改项目的启动对象为program类,代码如下:

public class program : Application
{
[STAThread]
static void Main()
{
//用户验证操作Start
//......
//用户验证操作Start //方法1
Application app = new Application();
Window1 win1 = new Window1();
app.Run(win1); //方法2
//Application app = new Application();
//Window1 win1 = new Window1();
//app.MainWindow = win1;
//win1.Show();
//app.Run(); //方法3
//Application app = new Application();
//app.StartupUri = new Uri("Window1.xaml", UriKind.Relative);
//app.Run();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295