WPF入門教程系列二——Application介紹

來源:互聯網
上載者:User

標籤:部分   介面   實現   功能   stat   copy   命令列   表單   如何   

一.Application介紹

WPF和WinForm 很相似, WPF與WinForm一樣有一個 Application對象來進行一些全域的行為和操作,並且每個 Domain (應用程式定義域)中僅且只有一個 Application 執行個體存在。和 WinForm 不同的是WPF Application預設由兩部分組成 : App.xaml 和 App.xaml.cs,這有點類似於 Asp.Net WebForm,將定義和行為代碼相分離。

微軟把WPF中經常使用的功能都封裝在 Application 類中了。 Application 類具體有以下功能: 

  • 跟蹤應用程式的生存期並與之互動。 
  • 檢索和處理命令列參數。 
  • 檢測和響應未處理的例外狀況。 
  • 共用應用程式範圍的屬性和資源。 
  • 管理獨立應用程式中的視窗。 
  • 跟蹤和管理導航。 

二.WPF應用程式的啟動

關於如何在Visual Studio中建立一個“WPF應用程式”,前面的文章中已經說過了。請參見WPF入門教程系列一——基礎。

1、在Visual Studio 2013中建立一個“WPF應用程式”,使用App.xaml檔案定義啟動應用程式。XAML從嚴格意義上說並不是一個純粹的 XML 格式檔案,它更像是一種 DSL(Domain Specific Language,領特定領域語言),它的所有定義都會由編譯器最後編譯成代碼。App.xaml檔案預設內容如:

 

2、當然,如果你習慣了把啟動寫在代碼中。你也可以跟WinForm中一樣,在類中定義一個Main方法來實現對WPF應用程式的啟動。

第一步:如,在“方案總管”中使用滑鼠左鍵選中App.xaml檔案,然後滑鼠右鍵,彈出“快顯功能表—》從項目中排除”,這樣就在項目中去掉了我們剛才建立的App.xaml檔案。

第二步:現在中添加一個新類,類名為App.cs。如。在“方案總管”中使用滑鼠左鍵選中“WpfApp1”項目,然後點擊滑鼠右鍵,彈出“快顯功能表”,按中所示操作。

 

 

第三步:在彈出的“添加新項”介面中,選中“類”,把名稱修改為“App.cs”。如。

 

第四步:在“方案總管”中雙擊App.cs(如),開啟檔案,然後可以分別寫入以下三種方式的代碼,進行啟動效果的調試。

 

第一種啟動應用程式的代碼

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows; namespace WpfApp1{    class App    { [STAThread]        static void Main()        {            // 定義Application對象作為整個應用程式入口              Application app = new Application();            // 方法一:調用Run方法 ,這種方式跟winform的調用一樣            WindowGrid win = new WindowGrid();            app.Run(win);                   }    }}

 

第二種啟動應用程式的代碼

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows; namespace WpfApp1{    class App    { [STAThread]        static void Main()        {            // 定義Application對象作為整個應用程式入口              Application app = new Application();                    //指定Application對象的MainWindow屬性為啟動表單,然後調用無參數的Run方法              WindowGrid win = new WindowGrid();              app.MainWindow = win;            //是必須的,否則無法顯示表單             win.Show();                                app.Run();          }    }  }

 

 

第三種啟動應用程式的代碼

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows; namespace WpfApp1{    class App    { [STAThread]        static void Main()        {            // 定義Application對象作為整個應用程式入口              Application app = new Application();            // 通過Url的方式啟動            app.StartupUri = new Uri("WindowGrid.xaml", UriKind.Relative);            app.Run();        }    }}

 

 

3. 上面的方法,最後執行的效果,如。

 

WPF入門教程系列二——Application介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.