5

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

king jia
  • 692
  • 8
  • 20
  • 43
  • Why are you trying to do it that way? Do you just want to have a console open on the side or is there a specific strategy behind this? – B.K. Nov 19 '15 at 02:49
  • Because initially my application is a console application. I try to add new argument to start WPF application in my console application. @B.K. – king jia Nov 19 '15 at 02:51

1 Answers1

9

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>
B.K.
  • 9,982
  • 10
  • 73
  • 105
  • Yeah. For this it is able to run with public partial class App : Window. but with public partial class App : Application, it will show Argument type 'WPF.app' is not assignable to parameter type 'System.Windows.Window'. – king jia Nov 19 '15 at 02:54
  • So I wondering what is the different between extend Application & Window – king jia Nov 19 '15 at 02:56
  • @kingjia App should not derive from Window. You should just have a Window to open. – B.K. Nov 19 '15 at 02:57
  • BK are right. You only can use console or wpf. No way to open two types on the same process. – Igor Quirino Nov 19 '15 at 03:05
  • @IgorQuirino Not sure what you mean by that. – B.K. Nov 19 '15 at 03:08
  • Ok, That's mean in order to start a WPF in console, we can only use Window instead of Application? Am I correct? – king jia Nov 19 '15 at 03:29
  • @kingjia No, you can use both. However, when you use Application.Run() method, it doesn't take another Application as argument. It only takes Window or nothing. If you pass nothing, it'll start, but nothing will show. If you pass a Window, it'll start a Window. However, if you declare a starting Window in your Application XAML, then it'll open a window that you've declared previously, if you use Application.Run() and pass nothing. There are different scenarios. – B.K. Nov 19 '15 at 03:32
  • @kingjia See another edit that shows one more method of opening a Window if you want one to be opened. – B.K. Nov 19 '15 at 03:34
  • Important - you MUST refernece to "PresentationFramework". – Ehud Grand Aug 07 '16 at 21:15
  • @user1223457 It's kind of a given, since you can't create a Window if you don't have the PresentationFramework.dll referenced. That's where the System.Windows namespace comes from. – B.K. Aug 07 '16 at 21:59
  • Not all the time. In my case I created a solutin with console application project and then added a wpf project. Took me some time to find all the neccesary dll's for it, so if anyone comes up with the same problem I'v written here the answer. – Ehud Grand Aug 08 '16 at 06:59
  • @user1223457 What I'm saying is that it's a given that you need that dll if you're planning on using the WPF framework. If you're working with WPF, there's an assumption that you took the time to learn where the code comes from. – B.K. Aug 08 '16 at 07:02