WPF Richtextbox XamlWriter 行为
我正在尝试将一些 C# 源代码保存到数据库中。基本上我有一个 RichTextBox,用户可以输入他们的代码并将其保存到数据库中。
当我从 Visual Studio 环境复制和粘贴时,我想保留格式等。因此我选择将 FlowDocuments Xaml 保存到数据库并将其设置回 RichTextBox.Document。
我的下面两个函数对 RTB 的内容进行序列化和反序列化。
private string GetXaml(FlowDocument document)
{
if (document == null) return String.Empty;
else{
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
XamlDesignerSerializationManager sm = new XamlDesignerSerializationManager(xw);
sm.XamlWriterMode = XamlWriterMode.Expression;
XamlWriter.Save(document, sm );
return sb.ToString();
}
}
private FlowDocument GetFlowDocument(string xamlText)
{
var flowDocument = new FlowDocument();
if (xamlText != null) flowDocument = (FlowDocument)XamlReader.Parse(xamlText);
// Set return value
return flowDocument;
}
但是,当我尝试序列化和反序列化以下代码时,我注意到这种不正确的(?)行为
using System;
public class TestCSScript : MarshalByRefObject
{
}
Serialized XAML is
使用 系统; 民众 类 TestCSScript : MarshalByRefObject {}{ }
注意新的一组“{}”
我在这里做错了什么...提前感谢您的帮助!
I am trying to save some c# source code into the database. Basically I have a RichTextBox that users can type their code and save that to the database.
When I copy and paste from the visual studio environment, I would like to preserve the formating etc. So I have chosen to save the FlowDocuments Xaml to the database and set this back to the RichTextBox.Document.
My below two function serialise and deserialise the RTB's contents.
private string GetXaml(FlowDocument document)
{
if (document == null) return String.Empty;
else{
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
XamlDesignerSerializationManager sm = new XamlDesignerSerializationManager(xw);
sm.XamlWriterMode = XamlWriterMode.Expression;
XamlWriter.Save(document, sm );
return sb.ToString();
}
}
private FlowDocument GetFlowDocument(string xamlText)
{
var flowDocument = new FlowDocument();
if (xamlText != null) flowDocument = (FlowDocument)XamlReader.Parse(xamlText);
// Set return value
return flowDocument;
}
However when I try to serialise and deserialise the following code, I am noticing this incorrect(?) behaviour
using System;
public class TestCSScript : MarshalByRefObject
{
}
Serialised XAML is
using
System;
public
class TestCSScript :
MarshalByRefObject
{}{
}
Notice the the new set of "{}"
What am I doing wrong here... Thanks in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在已经接受了一个迟缓的解决方案,但如果你们中有人找到了一个干净的解决方案,请发布它。
我使用 Stringbuilder 的 Replace 调用来删除不需要的字符。
我有一种感觉,当 xamlwriter 遇到“{”字符时,它会将其解释为绑定表达式。我想知道这个角色的转义序列是什么。
注意-我尝试更改,
但没有感到高兴。
I have resigned to a tardy solution for now, but if any of you finds a clean one, please post it.
I have used a Replace call of Stringbuilder to remove the undesired characters.
I have a feeling that when the xamlwriter encounters "{" character - it intreprets that as a binding expression. I wonder what the escape sequence is for this character.
Note - I tried changing the
with no joy.