WPF預設程式啟動:新建project後自動生成的App.xaml中指定程式啟動方式(StartupUri="MainWindow.xaml"),如下代碼所示,啟動MainWindow頁面 WPF用Main函數方式啟動程式:自己寫Main函數作為啟動點1.在WPF自動生成的App.cs文件中寫M.....
WPF預設程式啟動:新建project後自動生成的App.xaml中指定程式啟動方式(StartupUri="MainWindow.xaml"),如下代碼所示,啟動MainWindow頁面
<Application x:Class="MyFirstWPFApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyFirstWPFApplication"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
WPF用Main函數方式啟動程式:自己寫Main函數作為啟動點
1.在WPF自動生成的App.cs文件中寫Main函數
public partial class App : Application
{
[STAThread]
static void Main()
{
// 方式1
Application app = new Application();
MyWindow win = new MyWindow();
app.Run(win);
// 方式2
MyWindow win = new MyWindow ();
app.MainWindow = win;
win.Show();
app.Run();
// 方式3
app.StartupUri = new Uri("MyWindow.xaml", UriKind.Relative);
app.Run();
}
}
2.自己創建一個類,寫Main函數
public static class StartUpClass
{
// [STAThread] attribute指示應用程式的 COM線程模型是單線程單元
[STAThread]
public static void Main(string[] args)
{
SingleInstanceStart SingleInstanceStart = new SingleInstanceStart();
SingleInstanceStart.Run(args);
}
}
WindowsFormsApplicationBase需要引用Microsoft.VisualBasic.dll
public class SingleInstanceStart : WindowsFormsApplicationBase
{
protected override bool OnStartup(StartupEventArgs e)
{
// DoSomething
return true;
}
}
最後 需要更改App.xaml文件屬性
生成操作從ApplicationDefinition改為Page。