为什么在添加新文本时,线条之间有空行?

发布于 2025-01-27 12:12:01 字数 1086 浏览 3 评论 0原文

private void Println(string text, SolidColorBrush brush) => Dispatcher.Invoke(() =>
        {
            RichTextBoxLogger.Document.Blocks.Add(new Paragraph(new Run(text) { Foreground = brush }));
        });

        private void Println(string text)
        {
            Println(text, Brushes.LawnGreen);
        }

        private void PrintMsg(string message)
        {
            Println($"[+] {message}", Brushes.Yellow);
        }

        private void PrintErr(Exception e)
        {
            Println($"[-] {e}", Brushes.Red);
        }

使用它的

private void Fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            string time = DateTime.Now.ToString("h:mm:ss tt");

            string output = $"[*] {e.Name}: \"Deleted At : {time}\"";
            Println(output);
        }

结果是:

”它们之间添加了空线的线条”

我想在它们之间没有空线的情况下添加此行。

private void Println(string text, SolidColorBrush brush) => Dispatcher.Invoke(() =>
        {
            RichTextBoxLogger.Document.Blocks.Add(new Paragraph(new Run(text) { Foreground = brush }));
        });

        private void Println(string text)
        {
            Println(text, Brushes.LawnGreen);
        }

        private void PrintMsg(string message)
        {
            Println(
quot;[+] {message}", Brushes.Yellow);
        }

        private void PrintErr(Exception e)
        {
            Println(
quot;[-] {e}", Brushes.Red);
        }

Using it

private void Fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            string time = DateTime.Now.ToString("h:mm:ss tt");

            string output = 
quot;[*] {e.Name}: \"Deleted At : {time}\"";
            Println(output);
        }

The result is :

lines added with empty lines between them

I want to add this lines without empty lines between them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2025-02-03 12:12:02

为了防止在两个连续的段落之间发生中断,您必须将段落设置为属性属性为true

var paragraphWithoutBreak = new Paragraph { KeepWithNext = true };

我不建议使用重型richtextbox < /代码>用于您的任务。当输出生长时,它将变得慢。而是使用轻ListBoxListBox功能UI虚拟化,并将显着提高性能。当您将listBoxItem.ishittestvisible设置为false时,ListBox看起来和感觉就像是只读文档。为每种消息类型定义一个数据表板,以控制输出的外观:

ilogMessage.cs

interface ILogMessage
{
  string Message { get; }
}

errormessage.cs
红色消息。

class ErrorMessage : ILogMessage
{
  // TODO::Implement interface and add a constructor that accepts a message.
}

parningmessage.cs
黄色消息。

class WarningMessage : ILogMessage
{
  // TODO::Implement interface and add a constructor that accepts a message.
}

infomessage.cs
绿色消息。

class InfoMessage : ILogMessage
{
  // TODO::Implement interface and add a constructor that accepts a message.
}

mainwindow.xaml.cs

partial class MainWindow : Window
{
  public ObservableCollection<ILogMessage> Messages { get; }
  
  public MainWindow()
  {
    InitializeConmponent();
    this.Messages = new ObservableCollection<ILogMessage>();
  }

  private void WriteInfoLine(string message) 
    => this.Messages.Add(new InfoMessage(message));

  private void WriteWarningLine(string message) 
    => this.Messages.Add(new WarningMessage(message));

  private void WriteErrorLine(string message) 
    => this.Messages.Add(new ErrorMessage(message));
}

mainwindow.xaml

<Window>
  <Window.Resources>
    <DataTemplate DataType="{x:Type local:InfoMessage}">
      <TextBlock Text="{Binding Message, Mode=OneTime}"
                 Foreground="LawnGreen" />
    </DataTemplate>

    <DataTemplate DataType="{x:Type local:WarningMessage}">
      <TextBlock Text="{Binding Message, Mode=OneTime}"
                 Foreground="Yellow" />
    </DataTemplate>

    <DataTemplate DataType="{x:Type local:ErrorMessage}">
      <TextBlock Text="{Binding Message, Mode=OneTime}"
                 Foreground="Red" />
    </DataTemplate>
  </Window.Resources>

  <ListBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Messages}">
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="IsHitTestVisible"
                Value="False" />
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</Window>

strong 知道如何在添加新项目时将消息视图滚动到底部。您可以使用示例的logmessagebox控制(usercontrol),然后替换上述ListBox

To prevent a break from occurring between two consecutive paragraphs, you must set the Paragraph.KeepWithNext property to true:

var paragraphWithoutBreak = new Paragraph { KeepWithNext = true };

I don't recommend to use the heavy RichTextBox for your task. It will become slow when the output grows. Instead use a light ListBox. ListBox features UI virtualization and will significantly improve the performance. When you set ListBoxItem.IsHitTestVisible to false, the ListBox will look and feel like a read-only document. Define a DataTemplate for each message type to control the appearance of the output:

ILogMessage.cs

interface ILogMessage
{
  string Message { get; }
}

ErrorMessage.cs
A red message.

class ErrorMessage : ILogMessage
{
  // TODO::Implement interface and add a constructor that accepts a message.
}

WarningMessage.cs
A yellow message.

class WarningMessage : ILogMessage
{
  // TODO::Implement interface and add a constructor that accepts a message.
}

InfoMessage.cs
A green message.

class InfoMessage : ILogMessage
{
  // TODO::Implement interface and add a constructor that accepts a message.
}

MainWindow.xaml.cs

partial class MainWindow : Window
{
  public ObservableCollection<ILogMessage> Messages { get; }
  
  public MainWindow()
  {
    InitializeConmponent();
    this.Messages = new ObservableCollection<ILogMessage>();
  }

  private void WriteInfoLine(string message) 
    => this.Messages.Add(new InfoMessage(message));

  private void WriteWarningLine(string message) 
    => this.Messages.Add(new WarningMessage(message));

  private void WriteErrorLine(string message) 
    => this.Messages.Add(new ErrorMessage(message));
}

MainWindow.xaml

<Window>
  <Window.Resources>
    <DataTemplate DataType="{x:Type local:InfoMessage}">
      <TextBlock Text="{Binding Message, Mode=OneTime}"
                 Foreground="LawnGreen" />
    </DataTemplate>

    <DataTemplate DataType="{x:Type local:WarningMessage}">
      <TextBlock Text="{Binding Message, Mode=OneTime}"
                 Foreground="Yellow" />
    </DataTemplate>

    <DataTemplate DataType="{x:Type local:ErrorMessage}">
      <TextBlock Text="{Binding Message, Mode=OneTime}"
                 Foreground="Red" />
    </DataTemplate>
  </Window.Resources>

  <ListBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Messages}">
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="IsHitTestVisible"
                Value="False" />
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</Window>

See this example to know how to scroll the message view to the bottom when adding new items. You can use the example's LogMessageBox control (a UserControl) and replace the above ListBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文