5

我有一个像这样开头的 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 通过使用流文档引用 xaml 文件的物理路径,然后将数据解析到模板中。

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

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

string result = XamlWriter.Save(flowDocument)

并使用结果进行解析。

建议?

4

1 回答 1

6

如果我理解正确,您想从字符串中获取 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);
于 2009-05-22T12:48:31.450 回答