我试图从标准system.windows.controls.richtextbox控件中合并RTF上下文,并将其附加到Xamrichtexteditor Control的当前RTF上下文的末尾。当前,我的以下代码是在第一行上抛出零引用异常,我尝试引用临时XamrichTexteditor的" ActivedOcumentView",即使我在调用文档后会假设。负载应该初始化并用内容填充。我愿意从另一种方法来接触到这一点,似乎复制/粘贴的想法将是最简单的。
代码:
byte[] byteSig = Encoding.ASCII.GetBytes(rtfContextText);
using (MemoryStream ms = new MemoryStream(byteSig))
{
XamRichTextEditor tmpRichTextBox = new XamRichTextEditor();
tmpRichTextBox.Document.Load(RtfSerializationProvider.Instance, ms); // put current email body into memory stream
tmpRichTextBox.ActiveDocumentView.Selection.SelectAll(); // select all content
tmpRichTextBox.ActiveDocumentView.Selection.Copy(); // copy content into Clipboard
txtTextEditor.ActiveDocumentView.Selection.Paste(); // append Clipboard content into main XamRichTextEditor control
}
我能够为自己的问题构建答案,但是我永远无法获得复制和粘贴的方法来为我充分工作进入另一个,但在结果中似乎忽略了线路破裂和嵌入式图像(。
我最终必须直接与2个RichTextbox Contexts的原始数据手动合作。为此,我位于目标上下文中的位置,我想插入另一个,从其他上下文中,我剥离了除最外部的分组以外的所有(以避免复制根节点(。然后,我使用此串联版本将目标RichTextbox的文档加载。(下面的示例代码适用于我遇到同一问题的任何人。希望最终可以帮助某人。(
// get string encoded version of body
using (MemoryStream ms = new MemoryStream())
{
txtTextEditor.Document.Save(RtfSerializationProvider.Instance, ms);
ms.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(ms))
{
strRtfCurrBody = sr.ReadToEnd();
}
}
// find end and then the beginning of second-to-last grouping
strRtfSignature = strRtfSignature.Remove(strRtfSignature.LastIndexOf('}'));
int intCheck = PibsEmailer.RtfFindStartpoint(strRtfSignature, strRtfSignature.LastIndexOf('}'));
if (intCheck < 0)
{
// this should never happen unless the content of the signature is not in valid RTF format
throw new Exception("AddSignature Failed. Sig="" + Session.CurrentUser.EmailSignature + "" , Body="" + strRtfCurrBody + "" IntCheck=" + intCheck);
}
strRtfSignature = strRtfSignature.Substring(intCheck);
strRtfCurrBody = strRtfCurrBody.Insert(PibsEmailer.RtfFindLastParEnd(strRtfCurrBody), strRtfSignature);
byte[] byteFullContent = Encoding.ASCII.GetBytes(strRtfCurrBody);
using (MemoryStream ms = new MemoryStream(byteFullContent))
{
ms.Seek(0, SeekOrigin.Begin);
this.txtTextEditor.Selection.SelectAll();
this.txtTextEditor.Selection.Document.Load(RtfSerializationProvider.Instance, ms);
}
并有助于促进处理RTF格式数据的处理,我编写了以下两个功能(在上面的代码中引用(:
public static int RtfFindLastParEnd(string source)
{
int intLastPar = source.LastIndexOf("par}");
if (intLastPar > 0)
{
intLastPar += 4; // add offset of "par}"
}
return intLastPar;
}
public static int RtfFindStartpoint(string source, int indexOfClosingBrace)
{
string workingString = source;
int nestCount = 0;
int currIndex = indexOfClosingBrace;
while (currIndex > 0)
{
if (source[currIndex] == '}')
nestCount++;
else if (source[currIndex] == '{')
nestCount--;
if (nestCount == 0)
{
return currIndex;
}
currIndex--;
}
return -1;
}