如何使用Main()方法从没有App.xaml文件的代码启动UWP应用程序?

时间:2016-11-07 17:59:39

标签: uwp win-universal-app windows-10-universal uwp-xaml

通常,当您创建新的通用Windows应用程序时,Visual Studio会生成一个App.xaml文件,我假设它将在后台生成Main的应用程序启动方法(字符串args)。

我需要通过手动编写Main()方法在没有Xaml文件的情况下启动我的应用程序。

在WPF中我可以写下以下内容:

 [STAThread()]
 public static void Main() => new MyAppClass().Run();

但在UWP中, Windows.UI.Xaml.Application 类中没有 Run()方法。所以上面的工作没有用。

我应该编写什么代码来启动应用程序(假设我的应用程序类名为MyApplication,它继承自Windows.UI.Xaml.Application)?

1 个答案:

答案 0 :(得分:2)

发现它!

 static void Main(string[] args)
 {
     Windows.UI.Xaml.Application.Start((p) => new MyApplication());
 }