WPF 表單程式入口簡介

來源:互聯網
上載者:User

1,直接指定StartupUri為某一個window的子類Window1.xaml(屬性指定法)

<Application x:Class="brush.App"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    StartupUri="Window1.xaml">

 

2,通過Startup來指定當前隱藏CS代碼的類(brush.App類)的一個方法 MyApplication_Startup(事件指定法)

<Application x:Class="brush.App"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Startup="MyApplication_Startup" >

 

上面兩種方法都用到了App.xaml(前台表現) 及App.xaml.cs (後台實現),這就是一個Application的執行個體, 事實上在程式進入入口時已

啟動了Application執行個體。如果同時指定StartupUri及Startup,不會發生編譯錯誤,但會開啟兩個視窗。

 3.在後台編輯StartupUri及Startup

如,在InitializeComponent()方法中,設定了StartupUri屬性:

this.Startup += new System.Windows.StartupEventHandler(this.Application_Startup);

  this.StartupUri = new System.Uri("Window1.xaml",   System.UriKind.Relative);

它是對App.xaml中的Startup="Application_Startup”和StartupUri="Window1.xaml"翻譯。

4.在入口函數裡改

 

app.xaml中x:Class屬性把xaml所對應的後台C#類聯絡起來,在這裡是WpfApplication1.App,其中App是類名,WpfApplication1是命名空間。App.xaml在編譯後產生一個部分類,它位於所建立的項目目錄下的obj/Debug子目錄中,檔案名稱為App.g.cs。這個檔案中的內容如下:

 

using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1 {
   
   
    /// <summary>
    /// App
    /// </summary>
    public partial class App : System.Windows.Application {
       
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public static void Main() {
            WpfApplication2.App app = new WpfApplication1.App();
            app.Run();
        }
    }
}
可在Main函數裡面修改啟動項。注意,app裡面只能存在一個程式入口函數,即這裡的Main函數,否則會編譯錯誤。當然,你也完全可以把App.g.cs裡面的Main函數移到App.xaml.cs檔案裡面,因為他們都是部分類。這樣就可以通過入口函數來修改啟動表單。

文章來自網上及自己總結,如有錯誤,多多賜教。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.