zoukankan      html  css  js  c++  java
  • 【WPF】Application应用程序启动

    wpf应用程序在启动的时候会自动创建Main函数并调用Application实例的run(),从而启动Application进程。Main函数在一个App.g.cs文件中,App.g.cs文件的位置在objx86DebugApp.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();
            }
        }
  • 相关阅读:
    jquery获得url的get参数
    WampServer更改或重置数据库密码
    phpexcel乱码问题
    5kcrm增加权限管理中的模块(签到统计)
    windows关于定时执行的php脚本
    php 中引入邮箱服务 , 利用第三方的smtp邮件服务
    thinkphp 多个字段的不同关系的查询条件实现 .
    redis与memcached有什么区别
    MYSQL语句大全
    使用HttpClient工具类发起Restful API调用
  • 原文地址:https://www.cnblogs.com/lenlen-de/p/3422777.html
Copyright © 2011-2022 走看看