-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleTextToTextBox.cs
42 lines (35 loc) · 1.04 KB
/
ConsoleTextToTextBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.IO;
using System.Text;
using System.Windows.Controls;
public class ConsoleTextToTextBox : TextWriter
{
private TextBlock _textBlock;
private ScrollViewer _scrollViewer;
private Queue<string> _lines = new Queue<string>();
private readonly int _maxLines = 100;
public ConsoleTextToTextBox(TextBlock textBlock, ScrollViewer scrollViewer)
{
_textBlock = textBlock;
_scrollViewer = scrollViewer;
textBlock.Text = string.Empty;
}
public override Encoding Encoding => Encoding.UTF8;
public override void Write(char value)
{
Write(value.ToString());
}
public override void WriteLine(string value)
{
_textBlock.Dispatcher.Invoke(() =>
{
if (_lines.Count >= _maxLines)
{
_lines.Dequeue();
}
_lines.Enqueue(value);
_textBlock.Text = string.Join("\n", _lines);
// Ensure the ScrollViewer scrolls to the bottom
_scrollViewer.ScrollToEnd();
});
}
}