Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从DocumentViewer中删除FixedDocument?_C#_Wpf_Xps_Documentviewer - Fatal编程技术网

C# 如何从DocumentViewer中删除FixedDocument?

C# 如何从DocumentViewer中删除FixedDocument?,c#,wpf,xps,documentviewer,C#,Wpf,Xps,Documentviewer,我有一个带有fixedDocument(用XAML构建)的DocumentViewer,然后我用代码向fixedDocument添加内容,它在屏幕上完美地显示出来 我的问题是,当我试图从fixedDocument创建XPS文件时,我收到一个“它已经是另一个元素的子元素”错误 我找不到DocumentViewer.Children.Clear方法,如何删除/分离fixedDocument以便使用它创建文件 为了完整起见,下面是我得到错误的代码: public void CreateXPSF

我有一个带有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();
    }
public void CreateXPSFile()
{
//1-创建xps\u文件
字符串OutputPath=baseDir+pathAdjust+“test.xps”;
使用(FileStream fs=File.Create(OutputPath))
{
ConvertToXps(固定文件,fs);
}
//使用系统默认xps查看器打开文档
进程启动(OutputPath);
}
公共静态void ConvertToXps(FixedDocument fd,Stream outputStream)
{          
var package=package.Open(outputStream,FileMode.Create);
var xpsDoc=新的XpsDocument(package,CompressionOption.Normal);
XpsDocumentWriter=XpsDocument.CreateXpsDocumentWriter(xpsDoc);
//xps文档是使用固定的文档序列构建的
var fixedDocSeq=新的FixedDocumentSequence();
var docRef=新文档引用();
docRef.BeginInit();
文件参考设置文件(fd);
docRef.EndInit();

((IAddChild)fixedDocSeq.AddChild(docRef);您应该能够将文档设置为null

DocumentViewer dv;
dv.Document = null;

由于您没有将XPS加载到dv中,因此设置dv.Document=null;可能不起作用。请将XPS加载到dv中,而不是Process.Start(OutputPath)。或者您可以将该进程分配给一个名称,以便关闭它。但我会显式加载到dv中

        System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
        myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.Start();
        // ... 
        myProcess.Close();