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。
来源:https://www.cnblogs.com/chikenonachain/p/5135690.html