Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

c# - Start WPF Application in Console Application

Is it possible to start WPF Application in Console mode?

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }
}

<Application x:Class="WPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>

[STAThread]
static void Main(string[] args)
{
  if (args.Length > 0)
  {
     switch (args[0].ToLower())
     {
       case "/g": RunApplication(); break;
     }
  }
}

private static void RunApplication()
{
    var application = new System.Windows.Application();

    application.Run(new App());
}

It will show Argument type 'WPF.app' is not assignable to parameter type 'System.Windows.Window'.

Any solution to work around it?? Any different between

1.public partial class App : Application

2.public partial class App : Window

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could declare a Window and then start your app this way:

var application = new System.Windows.Application();
application.Run(new Window());

EDIT:

You seem a bit confused, so let me explain:

Say you have a program:

using System;

namespace ConsoleApplication
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            RunApplication();
        }

        private static void RunApplication()
        {
            var application = new System.Windows.Application();
            application.Run();
        }
    }
}

This will run a WPF application with no Window.

If, on the other hand, you pass a Window into application.Run(), you will get a WPF window. App should not derive from Window, since it should derive from Application.

Application.Run method either takes no arguments or a Window. It does not take Application. Therefore, if you want to start a previously created Application, as you have over there, you should do something like this:

private static void RunApplication()
{
    var application = new App();
    application.Run();  // add Window if you want a window.
}

Lastly, if you want to just use application.Run() and not have to pass a specific Window, just declare a starting Window in your Application XAML using StartupUri:

<Application x:Class="WPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="SomeWindow.xaml">
</Application>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...