0

私はfixedDocument(XAMLで構築された)を持つDocumentViewerを持っています。次にコードでfixedDocumentにコンテンツを追加すると、画面に完全に表示されます。

私の問題は、fixedDocument から XPS ファイルを作成しようとすると、「既に別の要素の子です」というエラーが表示されることです。

DocumentViewer.Children.Clear メソッドが見つかりません。fixedDocument を削除/デタッチして、それを使用してファイルを作成するにはどうすればよいですか?

完全を期すために、エラーが発生するコードは次のとおりです。

    public void CreateXPSFile()
    {
        // 1 - create xps_file          
        string OutputPath = baseDir + pathAdjust + "test.xps";
        using (FileStream fs = File.Create(OutputPath))
        {
            ConvertToXps(fixedDocument, fs);
        }

        // open the document using the system default xps viewer
        Process.Start(OutputPath);
    }

    public static void ConvertToXps(FixedDocument fd, Stream outputStream)
    {          
        var package = Package.Open(outputStream, FileMode.Create);
        var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
        XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);

        // xps documents are built using fixed document sequences
        var fixedDocSeq = new FixedDocumentSequence();
        var docRef = new DocumentReference();
        docRef.BeginInit();
        docRef.SetDocument(fd);
        docRef.EndInit();
        ((IAddChild)fixedDocSeq).AddChild(docRef); <<<<<----- Error occurs here

        // write out our fixed document to xps
        xpsWriter.Write(fixedDocSeq.DocumentPaginator);

        xpsDoc.Close();
        package.Close();
    }

ありがとう

4

2 に答える 2

2

Document を null に設定できるはずです。

DocumentViewer dv;
dv.Document = null;
于 2011-07-27T15:23:44.367 に答える
0

XPS を dv にロードしていないため、dv.Document = null; を設定します。うまくいかないかもしれません。Process.Start(OutputPath); ではなく xps を dv にロードします。または、プロセスを閉じることができるように、プロセスに名前を割り当てることができます。しかし、明示的に dv にロードします。

        System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
        myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.Start();
        // ... 
        myProcess.Close();
于 2011-07-27T20:17:23.470 に答える