简体   繁体   English

WPF RichTextBox:AppendText、TextRange 和尾随换行符

[英]WPF RichTextBox: AppendText, TextRange and the trailing newline

I am reading a text file's contents into a RichTextBox like this:我正在将文本文件的内容读入 RichTextBox,如下所示:

string contents = File.ReadAllText("MyFile.txt");
myRichTextBox.Document.Blocks.Clear();
myRichTextBox.AppendText(contents);

I am using the RichTextBox to automatically apply some syntax highlighting of sorts.我正在使用 RichTextBox 自动应用一些语法高亮显示。 When I try reading the unformatted text as described here to save it back to the file, things happen:当我尝试读取此处描述的未格式化文本以将其保存回文件时,会发生以下情况:

  1. A newline (\r\n) is added to the back of the file, which I don't want unless the user explicitly adds this newline.换行符 (\r\n) 被添加到文件的后面,除非用户明确添加此换行符,否则我不希望这样做。
  2. When I load the file again, the newline is not displayed in the RichTextEdit, even if it is present in the file.当我再次加载文件时,RichTextEdit 中不会显示换行符,即使它存在于文件中。

How can I change this, so that the RichTextBox displays and returns exactly the contents of the text file?如何更改此设置,以便 RichTextBox 显示并准确返回文本文件的内容?

Could this work?这能行吗? contents.Replace("\r\n", "\n");内容。替换(“\r\n”,“\n”);

The newline \r\n (CR/LF) is part of the text formatting in the RichTextBox control.换行符\r\n (CR/LF) 是RichTextBox控件中文本格式的一部分。 Each paragraph while converting to the text will be appended by the \r\n .转换为文本时的每个段落都将附加\r\n

This is means when a user press the ENTER button a new paragraph with \r\n is adding to the RichTextBox control.这意味着当用户按下ENTER按钮时,带有\r\n的新段落将添加到RichTextBox控件中。 And when StringFromRichTextBox() method, described in the Microsoft documentation is used to extract the text content from a RichTextBox it will return a string in which all paragraphs are separated by the \r\n .当 Microsoft 文档中描述的StringFromRichTextBox()方法用于从RichTextBox中提取文本内容时,它将返回一个字符串,其中所有段落都由\r\n分隔。

The explanations regarding the comments above:关于上述评论的解释:

A newline (\r\n) is added to the back of the file, which I don't want unless the user explicitly adds this newline.换行符 (\r\n) 被添加到文件的后面,除非用户明确添加此换行符,否则我不希望这样做。

A newline \r\n is adding to the end of the file only as a part of the each paragraph ending.换行符\r\n仅作为每个段落结尾的一部分添加到文件的末尾。

NOTE: If it is necessary to save and thereafter to load the saved document the TextRange.Save() and TextRange.Load() methods can be used:注意:如果需要保存并随后加载保存的文档,可以使用TextRange.Save()TextRange.Load()方法:

public void SaveRtf(RichTextBox rtb, string file)
{
    var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    using (var stream = new StreamWriter(file))
    {
        range.Load(stream.BaseStream, DataFormats.Rtf);
    }
}

public void LoadRtf(RichTextBox rtb, string file)
{
    var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    using (var stream = new StreamWriter(file))
    {
        range.Save(stream.BaseStream, DataFormats.Rtf);
    }
}

If to save the whole RuchTextBox content the new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text will be used than any text formatting after restoring will be lost.如果要保存整个RuchTextBox内容,则将使用new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text而不是恢复后的任何文本格式都将丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM