如何为 WPF 制作简单的只读输出日志控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41277425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:58:24  来源:igfitidea点击:

How to make Simple Read-Only Output Log Console for WPF?

c#wpfvisual-studio

提问by Matt McManis

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.

我有一个 WPF 应用程序,我想添加一个按钮来打开控制台窗口并显示 Console.WriteLine 消息。它不一定是一个 CMD 控制台,只是一个像模拟的显示框。

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:

我的想法是创建一个带有 ScrollViewer/TextBox 的 Window、Console.xaml 并使用类似的方法重定向 WriteLine,但我无法让它工作:

XAML

XAML

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

C#

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

我在 Photoshop 中制作的一个示例,其中包含了日志文本

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:

我试过这个解决方案,但我不知道如何使用它。我收到 OnStartup “找不到合适的方法来覆盖”错误:

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

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

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

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

回答by Matt McManis

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.

我使用 ScrollView TextBlock 和 Inlines.Add() 而不是 Console.WriteLine() 提出了一种非常粗略的方法,但它正在做我需要的。

Create Window - LogConsole

创建窗口 - LogConsole

XAML

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

启用向 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

将日志消息写入 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

日志控制台