WPF Richtextbox XamlWriter 行为

发布于 2024-08-28 18:07:05 字数 1712 浏览 3 评论 0原文

我正在尝试将一些 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 技术交流群。

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

发布评论

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

评论(1

天冷不及心凉 2024-09-04 18:07:06

我现在已经接受了一个迟缓的解决方案,但如果你们中有人找到了一个干净的解决方案,请发布它。

我使用 Stringbuilder 的 Replace 调用来删除不需要的字符。

    private string GetXaml(FlowDocument document)
    {
        if (document == null) return String.Empty;
        else
        {

            StringBuilder sb = new StringBuilder();
            using (XmlWriter xw = XmlWriter.Create(sb))
            {
                XamlDesignerSerializationManager sm = new XamlDesignerSerializationManager(xw);
                sm.XamlWriterMode = XamlWriterMode.Expression;

                XamlWriter.Save(document, sm);
            }
            sb.Replace("{}", "");
            return sb.ToString();
        }

    }

我有一种感觉,当 xamlwriter 遇到“{”字符时,它会将其解释为绑定表达式。我想知道这个角色的转义序列是什么。

注意-我尝试更改,

XamlWriterMode from XamlWriterMode.Expression to XamlWriterMode.Value

但没有感到高兴。

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.

    private string GetXaml(FlowDocument document)
    {
        if (document == null) return String.Empty;
        else
        {

            StringBuilder sb = new StringBuilder();
            using (XmlWriter xw = XmlWriter.Create(sb))
            {
                XamlDesignerSerializationManager sm = new XamlDesignerSerializationManager(xw);
                sm.XamlWriterMode = XamlWriterMode.Expression;

                XamlWriter.Save(document, sm);
            }
            sb.Replace("{}", "");
            return sb.ToString();
        }

    }

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

XamlWriterMode from XamlWriterMode.Expression to XamlWriterMode.Value

with no joy.

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