There is a WPF application. I have already written a lot of code and I would not want to create a console and transfer the code there. Is it possible to somehow turn my application into a console? Type of output in the properties of the project has already changed. Now, as I understand it, you need to change the entry point so that the window is not initialized (Correct, do not understand how to write the word (=). I tried to comment out the line:

InitializeComponent(); 

But the window still appeared ... Apparently not so I understand this line.

  • Видимо не так я понимаю эту строку. - yes, this is the initialization of the filling of the window, but the creation of the window itself and its display in another place - Andrey NOP
  • Hmm, like a normal question, why minusuet, comrades? - Andrey NOP

1 answer 1

You need to open App.xaml and uninstall the StartupUri installation:

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

You can delete classes with windows altogether, and in the file App.xaml.cs override the OnStartup method - this will be your entry point.

Well, and plus, if you want to output something to the console, then you need to switch its Output type in the Console Application in the project properties


Another, more cardinal way (and maybe more correct) is to create another class in the application, for example, Program and add the static Main method to it:

 public static void Main(string[] args) { } 

Then in the project properties change Startup object - specify this class.

In this case, the App class can also be deleted - there will be no sense from it.

  • Great! Thank! Cool when you know such trifles) - Alexander Lee
  • Um, another moment. Where are the call arguments to look for now?) - Alexander Lee
  • Everything. I'm stupid ... In OnStartUp) - Alexander Lee
  • Yes, in StartupEventArgs.Args . In general, anywhere in the program, you can get the parameters passed to the application by calling the Environment.GetCommandLineArgs() method. Please note that there in the zero element of the array goes the way of the application call, and the parameters start with index 1 - Andrey NOP
  • one
    I think the second option is simpler and more correct. Because On the first one I ran into a problem - I could not close the application. Thank you very much! - Alexander Lee