7

I have a WPF Application and I'd like to add a button that will open a Console window and display Console.WriteLine messages. It doesn't have to be a CMD Console, just a display box like an emulated one.

My idea was to create a Window, Console.xaml with a ScrollViewer/TextBox and redirect the WriteLine using something like this but I can't get it to work:

XAML

<ScrollViewer Name="Scroller" Margin="0" Background="#FF000128">
    <TextBlock Name="OutputBlock" Foreground="White" FontFamily="Consolas"/>
</ScrollViewer>

C#

System.Diagnostics.Debug.WriteLine("hello", console.OutputBlock.Text);

Constructor

public Console(MainWindow mainwindow)
{
    InitializeComponent();
    this.mainwindow = mainwindow;
}

An Example I made in Photoshop with made up log text

Example Log Console

I tried this solution, but I'm not sure how to use it. I get an OnStartup "no suitable method found to override" error:

https://stackoverflow.com/a/10416069/6806643

I also tried this solution. I copy exact but it is filled with errors:

https://stackoverflow.com/a/14957478/6806643

This code gives no errors but I don't know how it's supposed to be used:

https://stackoverflow.com/a/3058921/6806643

Community
  • 1
  • 1
Matt McManis
  • 4,475
  • 5
  • 38
  • 93
  • 1
    Errors, you say? How descriptive. – Abion47 Dec 22 '16 at 07:06
  • 1
    For the first link you provide, the error "no suitable method found to override" is likely to occur when you put the function at wrong place. Did you put it in the brackets of "public partial class App : Application"? – t.m. Dec 22 '16 at 07:07
  • @t.m. I have it in the right spot now in App : Application. When I first run the program the Console pops up for a few seconds then dissappears. How do I call it with a button? Will it also contain all WriteLines while it was closed? – Matt McManis Dec 22 '16 at 07:28
  • 1
    @MattMcManis do not call ConsoleHelper.FreeConsole(); until you are done with the console. – t.m. Dec 22 '16 at 07:31
  • @t.m. That solves it closing. I put the ConsoleHelper in the MainWindow and ConsoleHelper.AllocConsole(); and WriteLine in a Button. It opens the Console now when I click the Button. But it doesn't WriteLine "Start" like in the example. – Matt McManis Dec 22 '16 at 07:52
  • At the same solution you gave the link for, there is a comment by same guy saying "Hmmm...That worked fine for me in WinForms, but it seems WPF does something different. You might try AttachConsole (msdn.microsoft.com/en-us/library/windows/desktop/…), but you'll need to get a handle for the process that started your app (presumably cmd.exe) " – t.m. Dec 22 '16 at 08:05
  • @t.m.There's no explanation on how to set up AttachConsole. I also don't know if this console will WriteLine while closed or keep the text after closing it. I also can't use Console.WriteLine() and have to use System.Diagnostics.Debug.WriteLine(). – Matt McManis Dec 22 '16 at 08:16
  • @t.m. I made a mistake with naming my Window "Console" and it was interfering with Console.WriteLine. It now writes to the Console correctly. – Matt McManis Dec 22 '16 at 08:28

1 Answers1

7

A very crude way I've come up using ScrollView TextBlock and Inlines.Add() instead of Console.WriteLine(), but it's doing what I need.

Create Window - LogConsole

XAML

<Grid>
    <ScrollViewer Name="Scroller" Margin="0" Background="#FF000128">
        <TextBlock Name="OutputBlock"  Foreground="White" FontFamily="Consolas" Padding="10"/>
    </ScrollViewer>
</Grid>

Enable Pass Data to LogConsole

private MainWindow mainwindow;

public LogConsole() {}

public LogConsole(MainWindow mainwindow)
{
    InitializeComponent();

    this.mainwindow = mainwindow;
}

// Hide Window instead of Exiting
protected override void OnClosing(CancelEventArgs e)
{
    this.Hide();
    e.Cancel = true;
    base.OnClosing(e);
}

MainWindow

Method - Start Log Console Hidden

public MainWindow()
{
    InitializeComponent();

    // Start the Log Console
    StartLogConsole();
}

// Method
public void StartLogConsole()
{
    MainWindow mainwindow = this;
    console = new LogConsole(mainwindow);
    // window position
    console.Left = this.Left + 0;
    console.Top = this.Top + 0;
    console.Hide();
}

LogConsole console = new LogConsole();

Open Log Console Button

private void buttonLogConsole_Click(object sender, RoutedEventArgs e)
{
    // window position
    console.Left = this.Left + 0;
    console.Top = this.Top + 0;
    console.Show();
}

Write Log Message to ScrollView TextBlock

// Log Console Message
console.OutputBlock.Inlines.Add("Starting Application...\n");

It logs at all time while running in the background and open/close without losing text.

Log Console

Matt McManis
  • 4,475
  • 5
  • 38
  • 93