zoukankan      html  css  js  c++  java
  • WPF之application对象

    WPF:Application简介

    Application是一个地址空间,在WPF中应用程序就是在System.Windows命名空间下的一个Application实例。一个应用程序只能对应一个Application的实例,而Application的生命周期自然是从运用程序启动到终止的周期。

    与winform类似,WPF需要一个ApplicationL来全局的行为和操作,并且每个DOmain中只能有一个Application实例,和winform不同WPF默认有2部分组成:App.xaml和App.xaml.cs:

    WPF应用默认启动方式是由:XAML中的StartupUri

    自定义启动函数有三种常用的启动方法:

    1.默认方式StartupUri,我们新建一个WPF应用程序看下它的xaml文件,明显的看到它指向MainWindow窗体

    <Application x:Class="WPFApplication应用.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
             
        </Application.Resources>
    </Application>

    2.mainwindow属性启动窗体

    3.调用run方法,参数为启动的窗体对象

    我们删除app.axml文件,自定义来演示三种启动方式,首先新建一个类来做为启动项,引入System.Windows命名空间,然后继承Application对象:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    //引用
    using System.Windows;
    
    namespace WPFApplication应用
    {
        class StartMain:Application
        {
            [STAThread]
            static void Main()
            {
                ////方法一:调用run方法,参数为启动的窗体对象 ,也是最常用的方法
                Application app = new Application();
                Window1 win = new Window1();
                app.Exit += new ExitEventHandler(app_Exit);
                app.Run(win);
    
                // 方法二:指定application对象的mainwindow属性为启动窗体,然后调用无参数的run方法
                //Application app = new Application();
                //Window1 win = new Window1();
                //app.MainWindow = win;
                //win.Show();
                //app.Run(win);
    
                // 方法三:通过url的方式启动
                //Application app = new Application();
                //app.StartupUri = new Uri("Window1.xaml", UriKind.Relative);
    
                  //设置应用退出模式 
                //app.ShutdownMode = ShutdownMode.OnMainWindowClose;
                //app.Run();
            }
    
            static void app_Exit(object sender, ExitEventArgs e)
            {
                Console.WriteLine("app_Exit");
            }
        }
    }
    

      

     ////方法一:调用run方法,参数为启动的窗体对象 ,也是最常用的方法
                //Application app = new Application();
                //Window1 win = new Window1();
                //app.Exit += new ExitEventHandler(app_Exit);
                //app.Run(win);
    

     demo实例:

    http://www.cnblogs.com/BABLOVE/admin/Files.aspx

  • 相关阅读:
    [HAOI2015]树上染色 [树形dp]
    【luogu1052】 过河 [动态规划]
    【luogu4819】 [中山市选]杀人游戏 [tarjan 缩点]
    【luogu4185】 [USACO18JAN]MooTube [并查集]
    [国家集训队]墨墨的等式 [差分约束]
    【RMQ】
    [USACO05DEC] 布局 [差分约束]
    [SCOI2011]糖果 [差分约束]
    【POJ 1201】Intervals
    【luogu1993】 小K的农场 [差分约束]
  • 原文地址:https://www.cnblogs.com/BABLOVE/p/3231315.html
Copyright © 2011-2022 走看看