方案一,更改启动项
出问题应用的启动项是使用的默认设置,查看App.g.cs文件,可以看到自动生成的Main入口函数,如下:
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(“PresentationBuildTasks”, “4.0.0.0”)]
public static void Main()
{
WpfApp1.App app = new WpfApp1.App();
app.InitializeComponent();
app.Run();
}
新建一个StartUp文件,将项目启动项修改为WpfApp1.StartUp,这样就可以解决该问题,代码如下。与原始方案对比,该方案使用了WindowsFormsApplicationBase,该方案为什么可以解决启动慢的问题,我还没搞清楚,或许和方案二有关。
// StartUp.cs
class StartUp : Application
{
[STAThread]
public static void Main(string[] args)
{
var startUp = new MyStartUp();
startUp.Run(args);
}
}
// 需要添加对Microsoft.VisualBasic的引用
class MyStartUp : WindowsFormsApplicationBase
{
protected override