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檔案裡面,因為他們都是部分類。這樣就可以通過入口函數來修改啟動表單。
文章來自網上及自己總結,如有錯誤,多多賜教。