Getting a flowdocument from a xaml template file

爱⌒轻易说出口 提交于 2019-12-23 11:39:08

问题


I got a Xaml file that starts like this:

  <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"

Current solution uses a StremReader by referring to the physical path of the xaml file with the flowdocument and then parses data into the template.

This is not a valid solution, so I need to get the flowdocument withouth referring to the physical path.

I would like to use the xmlns namespace or similar in my C# code and do like

string result = XamlWriter.Save(flowDocument)

And use the result for parsing.

Suggestions?


回答1:


If I understand correctly, you want to get the FlowDocument from a string? You can do this with XamlReader.Parse:

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

EDIT: If the XAML file is a part of your project, you can mark it as EmbeddedResource and use the following to load it:

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


来源:https://stackoverflow.com/questions/897505/getting-a-flowdocument-from-a-xaml-template-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!