• 【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();
            }
        }
  • 相关阅读:
    如何给wordpress外部链接自动添加nofollow
    wordpress如何批量关闭旧日志留言功能
    如何一次把所有wordpress插件都禁用了
    sql批量获取wordpress所有留言者的邮件地址
    wordpress数据库优化-关闭日志修订
    wordpress数据库优化wp_posts表 OPTIMIZE
    sql批量删除wordpress所有日志修订revision
    sql删除wordpress没用的postmeta记录
    wordpress如何删除没有文章的tags标签
    批量删除wordpress垃圾评论留言
  • 原文地址:https://www.cnblogs.com/lenlen-de/p/3422777.html
Copyright © 2020-2023  润新知