2

後で FlowDocument として FlowDocumentReader にロードできる .xaml ファイルに変換する必要があるデータのコレクションがあります。Paragraphs、Runs を直接インスタンス化するのではなく、後でドキュメントを作成するために xaml を生成します。

私が試したこと:

Paragraphs、Runs、InlineUIContainers などの XElements を作成してデータを反復処理し、FlowDocument 構造を適切に構築してから、次のように呼び出します。

XmlWriter writer = XmlWriter.Create("output.xaml");
flowDocElem.WriteTo(writer);
writer.Close();

消費アプリでは、次のようにします。

flowDocument = XamlReader.Load(xamlFile) as FlowDocument;
flowDocumentReader.Document = flowDocument;
xamlFile.Close();

しかし、FlowDocument が何であるかがわからないため、読み込みは失敗します。FlowDocument 要素は次のようになります。

<FlowDocument Name="testDoc">

(読み込まれたときに FlowDocument が何であるかを明らかにするための名前空間はありません。)

.xaml を手動で編集し、要素を次のように変更すると:

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Name="testDoc">

その後、問題なくロードされます。

FlowDocument の XElement を作成するときに、次のことを試みました。

new XElement("FlowDocument", new XAttribute("Name", "testDoc"), new XAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"));

名前空間属性を作成しようとすると、エラーが発生します。

そのxmlnsを要素に完全にチートして詰め込み、次のようなものを呼び出すことができます

File.WriteAllText("output.xaml", fixedTxt);

しかし、それは汚いと感じるので、単に間違っていると思います。

考え?


アップデート:

これはおそらく問題に対する規範的な解決策ではありませんが、機能します。

ParserContextを XamlReader に 追加することで、FlowDocument xml の読み込みに関する問題を回避することができました。

FileStream xamlFile = new FileStream("output.xaml", FileMode.Open, FileAccess.Read);
XamlReader x = new XamlReader();
ParserContext parserContext = new ParserContext();
parserContext.XmlnsDictionary.Add("","http://schemas.microsoft.com/winfx/2006/xaml/presentation");
flowDocument = XamlReader.Load(xamlFile, parserContext) as FlowDocument;
flowDocumentReader.Document = flowDocument;
xamlFile.Close();
4

2 に答える 2

2

XamlWriterの代わりに使ってみてくださいXmlWriter

于 2011-01-25T16:13:02.827 に答える
0

XLinq を使用する場合は、次のことを試してください。

XNamespace ns = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace xns = @"http://schemas.microsoft.com/winfx/2006/xaml";
XElement someElement = new XElement(ns + "FlowDocument",
                           new XAttribute(xns + "Name", name),
                           ...);
于 2011-01-25T19:52:15.437 に答える