I’ve had several WPF projects which required showing a log window. The idea is to log a constant stream of status messages for a admin to monitor. You could write your own LogWindow and pass messages into the window but there is an easier way. Tell your WPF application to run as a ‘Console’ application.
Now this sounds funny but it is easy to do and doesn’t impact your WPF application.
- Right click the Project Properties node in the Solution explorer or choose the Project menu.
- Change the project Application Type to ‘WPF Console Application.
Run the application, you will see two windows; the main WPF window and the console window.
Using Console
To write to the console window use Console.Write or Console.WriteLine. You can also control the size and position of the window and change the font color.
1: Console.SetWindowSize(40, 20)2: Console.SetWindowPosition(0, 0)3: Console.Title = "Log Window"
4:5: Console.ForegroundColor = ConsoleColor.Yellow6: Console.WriteLine("Log: Something happened")
7: Console.ForegroundColor = ConsoleColor.Red8: Console.WriteLine("Details: Database initialized")
In both my copy of Studio 2008 Standard Edition and my works copy which is Professional Edition, there is not a “WPF Console Application” listed, just a “Console Application”, however this still works.
There isn’t a WPF console applcation. You need to create a WPF or Windows Forms application then switch to Console app in the project properties dialog
This was interesting. Thanks.
Anyone trying this should be aware that using Console.Readline will tie up the UI thread and make your window unresponsive, unless you do some nifty multi-threading.
[…] I could write to it and call Console.Clear() without any exceptions. (the basic idea is explained here, although my properties UI was slightly different than what is […]