我试图找到一种方法,我可以读取一个文本文件,然后添加文本文件的分词,所以我可以说*好的正常文本,直到你看到|
之后大胆直到你看到另一个|
之后再次使文本正常。但是我无法弄清楚如何在部分内容中完成这一点,但它就是整个部分,但就是这样。
文本文件的外观示例:在这里,我希望这个|*~b^works|
看起来不错!
private static TextPointer GetPoint(TextPointer start, int x)
{
var ret = start;
var i = 0;
while (i < x && ret != null)
{
if (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text ||
ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None)
{
i++;
}
if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
{
return ret;
}
ret = ret.GetPositionAtOffset(1,LogicalDirection.Forward);
}
return ret;
}
public void LetsSave()
{
string end = new TextRange(box.Document.ContentStart, box.Document.ContentEnd).Text;
Clipboard.SetText(end);
box.Document.Blocks.Clear();
box.Document.Blocks.Add(new Paragraph(new Run(richText)));
richText = new TextRange(box.Document.ContentStart, box.Document.ContentEnd).Text;
File.WriteAllText(@"C:\Users\jacob\Documents\test\test.txt", end);
string file = File.ReadAllText(@"C:\Users\jacob\Documents\test\test.txt");
var xx = file.Split('|');
box.Document.Blocks.Clear();
int length = 0;
int wordl = 0;
foreach (var c in xx)
{
wordl = c.Length;
var start = box.Document.ContentStart;
var startPos = GetPoint(start, length);
var endPos = GetPoint(start, length + wordl);
var textRange = box.Selection;
textRange.Select(startPos, endPos);
TextDecorationCollection tdc = textRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection;
MessageBox.Show(c);
box.AppendText(c);
if (c.Contains("*~"))
{
tdc.Add(myUnderline);
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, tdc);
}
length += c.Length;
}
}
答案 0 :(得分:0)
这Website帮助我解决了一个非常简单的问题。我使用的是.txt文件,我本应该使用.xaml。这是我最终在网站上使用的完成代码。
private static void LoadFile(string filename, RichTextBox richTextBox)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException();
}
if (!File.Exists(filename))
{
throw new FileNotFoundException();
}
// open the file for reading
using (FileStream stream = File.OpenRead(filename))
{
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
// sniff out what data format you've got
string dataFormat = DataFormats.Text;
string ext = System.IO.Path.GetExtension(filename);
if (String.Compare(ext, ".xaml", true) == 0)
{
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == 0)
{
dataFormat = DataFormats.Rtf;
}
documentTextRange.Load(stream, dataFormat);
}
}
private static void SaveFile(string filename, RichTextBox richTextBox)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException();
}
// open the file for reading
using (FileStream stream = File.OpenWrite(filename))
{
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
// sniff out what data format you've got
string dataFormat = DataFormats.Text;
string ext = System.IO.Path.GetExtension(filename);
if (String.Compare(ext, ".xaml", true) == 0)
{
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == 0)
{
dataFormat = DataFormats.Rtf;
}
documentTextRange.Save(stream, dataFormat);
}
}