在WPF的RichTextBox中打印日志的步骤如下
- 拖动一个RichTextBox控件到窗口中
- 改名为textLog
x:Name="textLog"
- 设置ReadOnly属性为True
IsReadOnly="True"
- 新建Log.cs文件并粘贴下面的代码到文件中
- 设置背景颜色为蓝色
Background="AliceBlue"
- 设置滚动条
VerticalScrollBarVisibility="Visible"
- 将RichTextBox的引用注入到Log.cs中
Log.SetTextControl(textLog)
Log.cs代码
using System;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
public static class Log
{
private const int MaxCount = 1000;
private static int Count = 0;
private static RichTextBox textControl;
private static InlineCollection inlines;
//设置主控件
public static void SetTextControl(RichTextBox _textBox)
{
textControl = _textBox;
Paragraph graph = new Paragraph();
inlines = graph.Inlines;
textControl.Document.Blocks.Add(graph);
}
//输出黑色消息
public static void Info(string format, params object[] args)
{
AppendText(Brushes.Black, format, args);
}
//输出绿色消息
public static void Suc(string format, params object[] args)
{
AppendText(Brushes.DarkGreen, format, args);
}
//输出黄色消息
public static void Warning(string format, params object[] args)
{
AppendText(Brushes.DarkOrange, format, args);
}
//输出红色消息
public static void Error(string format, params object[] args)
{
AppendText(Brushes.Red, format, args);
}
//清除日志
public static void Clear()
{
Count = 0;
inlines.Clear();
textControl.ScrollToEnd();
}
private static void AppendText(Brush color, string format, params object[] args)
{
textControl.BeginChange();
StringBuilder builder = new StringBuilder();
builder.Append("[");
builder.Append(Count++);
builder.Append("] : ");
builder.Append(string.Format(format, (object[])args));
builder.Append("\n");
string str = builder.ToString();
inlines.Add(new Run(str) { Foreground = color });
if (inlines.Count > MaxCount)
{
inlines.Remove(inlines.FirstInline);
}
textControl.ScrollToEnd();
textControl.EndChange();
}
}