【问题标题】:Getting a flowdocument from a xaml template file从 xaml 模板文件中获取流文档
【发布时间】:2011-07-31 05:21:07
【问题描述】:

我有一个这样开头的 Xaml 文件:

  <FlowDocument
       x:Name="flowDocument"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:Drawing="clr-namespace:System.Drawing;assembly=System.Drawing"

目前的解决方案是使用 StremReader,通过 flowdocument 引用 xaml 文件的物理路径,然后将数据解析到模板中。

这不是一个有效的解决方案,所以我需要在不参考物理路径的情况下获取流文档。

我想在我的 C# 代码中使用 xmlns 命名空间或类似名称并喜欢

string result = XamlWriter.Save(flowDocument)

并使用结果进行解析。

建议?

【问题讨论】:

    标签: wpf xaml flowdocument


    【解决方案1】:

    如果我理解正确,您想从字符串中获取 FlowDocument 吗?你可以通过XamlReader.Parse 做到这一点:

    string result = XamlWriter.Save(flowDocument);
    FlowDocument new_doc = (FlowDocument)XamlReader.Parse(result);
    

    编辑:如果 XAML 文件是您项目的一部分,您可以将其标记为 EmbeddedResource 并使用以下内容来加载它:

    Stream doc_stream = Assembly.GetExecutingAssembly()
                                .GetManifestResourceStream("YourNamespace.YourFile.xaml");
    FlowDocument doc = (FlowDocument)XamlReader.Load(doc_stream);
    

    【讨论】:

    • 抱歉我的问题的笨拙介绍。我确实有一个带有 flowdocument xaml 的 xaml 文件。我想了解 xaml 文件的内容,字符串会非常好。
    • 如果您的 XAML 是项目的一部分,则可以使用资源方法。否则,您必须以某种方式在磁盘上找到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多