C# 在文档查看器中显示 XPS 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1187752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:21:03  来源:igfitidea点击:

Displaying an XPS document in Document Viewer

c#wpfxpsdocumentviewer

提问by SumGuy

I'm having a go with document viewer and XPS atm as I haven't tried it before. So I have a simple piece of code loading an XPS document and displaying it in the document viewer, however the document doesn't appear. The document viewer loads and a quick step through in debug mode tells me the information is there,it just won't show.

我正在尝试使用文档查看器和 XPS atm,因为我以前从未尝试过。所以我有一段简单的代码加载一个 XPS 文档并在文档查看器中显示它,但是该文档没有出现。文档查看器加载并在调试模式下快速浏览告诉我信息在那里,只是不会显示。

        dvDoc = new DocumentViewer();

        string fileName = null;
        string appPath = System.IO.Path.GetDirectoryName(Assembly.GetAssembly(typeof(DocumentWindow)).CodeBase);

        if (type == "About")
            fileName = appPath + @"\Documents\About.xps";

        fileName = fileName.Remove(0, 6);
        XpsDocument doc = new XpsDocument(fileName, FileAccess.Read);

        dvDoc.Document = doc.GetFixedDocumentSequence();

All literature I can find tells me to do it this way yet it doesn't seem to work for me. I'm aware that document viewer doesn't like URI's, hence the filename.remove line.

我能找到的所有文献都告诉我要这样做,但它似乎对我不起作用。我知道文档查看器不喜欢 URI,因此 filename.remove 行。

Any suggestions as to what I'm missing.

关于我所缺少的任何建议。

Cheers, SumGuy

干杯,SumGuy

采纳答案by Chris Persichetti

You've probably already figured this out by now since it's been almost a month.

你现在可能已经想通了,因为已经快一个月了。

It doesn't look like your document viewer is part of your xaml file. It looks like you are creating a new DocumentViewer object, but never adding it to the xaml file.

看起来您的文档查看器不是您的 xaml 文件的一部分。看起来您正在创建一个新的 DocumentViewer 对象,但从未将其添加到 xaml 文件中。

Instead of

代替

dvDoc = new DocumentViewer();

Declare it in your xaml file:

在您的 xaml 文件中声明它:

<DocumentViewer Name="dvDoc" />