是否有像 Visual Studio 调试输出这样的 WPF 输出监视器控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13933958/
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 06:40:02  来源:igfitidea点击:

Is there a WPF Output Monitor Control like the Visual Studio Debug Output

c#wpfvisual-studiowpf-controls

提问by Bluuu

I am trying to add a output monitor to my WPF application. A read only monitor similar to the debug output in visual studio.

我正在尝试向我的 WPF 应用程序添加一个输出监视器。类似于 Visual Studio 中的调试输出的只读监视器。

Is there a WPF control which already provides the functionality I need? Or is there a way I could reuse the control from Visual Studio?

是否有一个 WPF 控件已经提供了我需要的功能?或者有什么方法可以重用 Visual Studio 中的控件?

Currently I'm using a standard TextBox backed by a StringBuilder. Updates go to the StringBuilder while the TextBox gets the newest string every 200ms.

目前我正在使用由 StringBuilder 支持的标准 TextBox。更新进入 StringBuilder,而 TextBox 每 200 毫秒获取一次最新的字符串。

My problem is that this gets really slow as the output string gets longer.

我的问题是,随着输出字符串变长,这会变得非常慢。

采纳答案by AkselK

I would use the RichTextBox control to output the data.

我会使用 RichTextBox 控件来输出数据。

In this sample I had no problem with performance at all.

在这个示例中,我的性能完全没有问题。

public partial class MainWindow : Window
{
    private int counter = 0;
    public MainWindow()
    {
        InitializeComponent();
        Loaded+=OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {

        for (int i = 0; i < 200; i++)
        {
            AddLine(counter++ + ": Initial data");
        }

        var timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
        timer.Tick += TimerOnTick;
        timer.IsEnabled = true;
    }

    private void TimerOnTick(object sender, EventArgs eventArgs)
    {
        AddLine(counter++ + ": Random text");
    }

    public void AddLine(string text)
    {
        outputBox.AppendText(text);
        outputBox.AppendText("\u2028"); // Linebreak, not paragraph break
        outputBox.ScrollToEnd();
    }
}

And XAML

和 XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <RichTextBox x:Name="outputBox"
                     VerticalScrollBarVisibility="Visible" 
                     HorizontalScrollBarVisibility="Visible" 
                     IsReadOnly="True">
            <FlowDocument/>
        </RichTextBox>

    </Grid>
</Window>

And it's probably easy to extend it. If the scroll position is not at the end, do not Scroll to the end, for example, so that you can view old data while the text box is still updating.

扩展它可能很容易。例如,如果滚动位置不在末尾,则不要滚动到末尾,以便在文本框仍在更新的同时查看旧数据。