FirstStep FirstStep - 2 months ago 11
C# Question

Why would a Console Application pops when reloading the Designer in Visual Studio 2015?

I am having this weird behavior:

Yesterday I was working on a WPF application on Visual Studio 2015 and I wanted to write some values to a console prompt while debugging so I included a console with my form.

Today, when I started visual studio, even without Starting the program, a console window pops up. This is weird. When traced it, it turned to be coming from loading the designer of visual studio:

Anyone has any idea why is that happening and how to fix it?

enter image description here

When I Disable Running Project Code I get this form where my ImageConrol looks like is kind of disabled, but the console never pops again:

enter image description here

Evk Evk
Answer

To display your control, WPF designer actually runs your code (not all of it, but still). So designer might execute your AllocConsole call, which leads to behavior you observe. To avoid this, do:

if (!DesignerProperties.GetIsInDesignMode(new DependencyObject())) {
    AllocConsole();
    Console.WriteLine("test");
}