【WPF】Application应用程序启动

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。

第一步:我们可以在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类,默认(未设置)

重新生成就可以了。

  各个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();
        }
    }

转载于:https://www.cnblogs.com/lenlen-de/p/3422777.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值