Silverlight裡面有個App.xaml和App.xaml.cs,是幹什麼的,用途是什麼,怎麼和它傳遞參數呢?
使用它裡面的public參數很簡單,假設在MainPage.xaml.cs你可以這樣訪問:
(Application.Current as App).YourPublicParam
同樣,在Silverlight Class Library類庫項目中你也可以用上面的方法訪問App裡面的變數,因為Application.Current是application全域的。
App.xaml來源
App.xaml和App.xaml.cs存在於Silverlight和WPF項目中,它來源於System.Windows.Application類庫,如果熟悉Winform開發的就應該對這個東西比較熟悉。當Silverlight Application啟動的時候,順序是這樣的:
沒有 Main() 方法 啟動 App 構造器
- 把 Application_Startup 事件handler綁定到App.Startup屬性
- 把Application_Exit 事件handler綁定到App.Exit 屬性
- 把Application_UnhandledException事件handler綁定到UnhandledException 屬性
- 觸發InitializeComponent
Application_Startup
- 建立新的 Page 對象執行個體
- 把 App.RootVisual 指向這個Page對象
InitializeComponent
- 調用Application.LoadComponent, 傳遞Uri 對象到 App.xaml
App.xaml用途
定義全域資源:我們可以把Application層級的資源放在這裡,用Application.Resources屬性訪問,常用的有styles, data templates,control templates。 全域事件處理:Startup, Exit 和UnhandledException. 全域引用Application.Current 指定RootVisible: RootVisual 用來設定Silverlight啟動控制項,這個屬性在 Startup 事件中設定. 本地安裝application:使用Install 方法你可以讓使用者在瀏覽器外本地安裝application 檢查更新:本地安裝的時候可以用CheckAndDownloadUpdateAsync 方法檢查.xap檔案是否最新