c# - 替换 XamlPackage 中的文本

标签 c# wpf string flowdocument

我在 RichTextBox 中有一些文本。此文本包含标签,例如:[@TagName!]。我想用数据库中的一些数据替换这些标签而不丢失格式(字体、颜色、图像等)。我创建了一个方法:

 void ReplaceTagsWithData(FlowDocument doc)
    {
        FileStream fs = new FileStream("tmp.xml", FileMode.Create);
        TextRange trTextRange = 
            new TextRange(doc.ContentStart, doc.ContentEnd);

        trTextRange.Save(fs, DataFormats.Xaml);
        fs.Dispose();
        fs.Close();

        StreamReader sr = new StreamReader("tmp.xml");

        string rtbContent = sr.ReadToEnd();

        MatchCollection mColl = 
            Regex.Matches(rtbContent, 
                          string.Format(@"\{0}+[a-zA-Z]+{1}", 
                          prefix, 
                          postfix));

        foreach (Match m in mColl)
        {
            string colname = 
                m.Value.Substring(prefix.Length, 
                   (int)(m.Value.Length - (prefix.Length + postfix.Length)));

            rtbContent = rtbContent.Replace(m.Value.ToString(), 
                                            dt.Rows[0][colname].ToString());
        }

        rtbEdit.Document = 
            new FlowDocument(
                (Section)XamlReader.Load(
                    XmlReader.Create(new StringReader(rtbContent))));
        sr.Dispose();
        sr.Close();
    }

它非常好,但它会从内容中删除图像。我知道我应该使用 XamlPackage 而不是 Xaml,但我无法将其作为纯文本获取。还有其他解决方案吗?

感谢您的回答。 ;)

[编辑:2012 年 2 月 13 日 02:14(上午)]

我的工作解决方案:

    void ReplaceTagsWithData(RichTextBox rtb)
{
    FlowDocument doc = rtb.Document;

    FileStream fs = new FileStream("tmp", FileMode.Create);
    TextRange trTextRange = new TextRange(doc.ContentStart, doc.ContentEnd);
    trTextRange.Save(fs, DataFormats.Rtf);
    fs.Dispose();
    fs.Close();

    StreamReader sr = new StreamReader("tmp");
    string rtbContent = sr.ReadToEnd();
    sr.Dispose();
    sr.Close();

    MatchCollection mColl = 
        Regex.Matches(rtbContent, 
                      string.Format(@"\{0}+[a-zA-Z]+{1}", 
                      prefix, 
                      postfix));

    foreach (Match m in mColl)
    {
        string colname = 
            m.Value.Substring(prefix.Length, 
                (int)(m.Value.Length - (prefix.Length + postfix.Length)));

        rtbContent = rtbContent.Replace(m.Value.ToString(), 
                                        dt.Rows[0][colname].ToString());
    }
    MemoryStream stream = 
        new MemoryStream(ASCIIEncoding.Default.GetBytes(rtbContent));
    rtb.SelectAll();
    rtb.Selection.Load(stream, DataFormats.Rtf);

}

也许它不是最好的,但它工作正常。

解决了。但是我无法发布解决方案,因为它位于我无法再访问的公司服务器上。

最佳答案

您可以使用 Razor 引擎在模板主题中做任何您想做的事情。您可以从 nuget ( http://www.nuget.org/packages/RazorEngine ) 下载,无需任何设置配置,您可以使用 Razor 语法来执行此操作。例如你的模板可以是这样的:

<Window x:Class="<class>"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="@Model.Title"
        Icon="@Model.Icon">
  <Grid>    
  </Grid>
</Window>

注意:@Model.Title 和@Model.Icon 来自 Razor

实际上我使用 RazorEngine 来完成我所有的模板任务: 电子邮件、即时生成报告 (rdlc) 等...

关于c# - 替换 XamlPackage 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9228414/

相关文章:

c# - 了解为什么 TPL 任务可以使用 OUT FromCurrentSynchronizationContext 更新 UI

android - 在 Android 中,_id 是否必须存在于任何创建的表中?

c# - 替换字符串中的某个子字符串 - .Replace() 方法对我不起作用 C#

wpf - IDataErrorInfo - 最初忽略首次加载 View 时的错误

c# - 如何执行DOS命令并检索结果

c# - 用正则表达式限制字数

c# - MVC4 自定义表单例份验证 - 无法将 System.Web.HttpContext.Current.User 转换为 CustomPrincipal

c# - 如何使用 WPF Canvas 以编程方式将图像从一点动画化到另一点?

string - Bash 脚本捕获输出到终端

C#基础理解题