简体   繁体   中英

Create a WPF window from command prompt application

Is it possible to create WPF window from a command prompt application?

for example I have a MainWindow WPF class with contains the main windows of my application. when I use the following code in my command prompt application I get this error: "The calling thread must be STA".

class Program
{        
    static void Main(string[] args)
    {
         MainWindow main = MainWindow();
         main.Show();
    }
}

I really need to create the window in my command prompt application but i don't know if its possible. please guide me how to do this.

regards

You can correct that error by marking the Main method with a STAThreadAttribute . You will also need to start a message pump by calling Application.Run . For example:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
         MainWindow main = new MainWindow();
         main.Show();
         new Application().Run();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM