WPF / Console Hybrid Application

后端 未结 4 1898
长发绾君心
长发绾君心 2021-02-13 01:27

I writing an application what can either be run on the command line, or with a WPF UI.

[STAThread]
static void Main(string[] args)
{
    // Does magic parse args         


        
4条回答
  •  失恋的感觉
    2021-02-13 02:15

    You can conditionally start your WPF application by performing the following steps with the sample below.

    1. Add another entry point with the 'Main' method declarated with STAThreadAttribute. This is required by WPF.
    2. Under your project's 'Build' properties, choose 'Console Application' as your output and your new 'Main' method as the application's 'Startup Object'.

      using System;
      
      public static class Program
      {
          [STAThreadAttribute]
          public static void Main()
          {
              Console.WriteLine("What now?");
              Console.ReadKey(true);
      
              App.Main();
          }
      }
      

提交回复
热议问题