【发布时间】:2015-09-01 04:36:51
【问题描述】:
我需要将数据库中的数据显示到 WPF 应用程序中并将其保存为 XPS 文档。我想将其显示为主窗口的一部分(带有工具栏、菜单和状态栏),而不是作为新窗口。
我应该使用什么控件?目前,我正在查看 FixedDocument 和 FlowDocument。我在正确的轨道上吗?有什么好的入门资料吗?
【问题讨论】:
我需要将数据库中的数据显示到 WPF 应用程序中并将其保存为 XPS 文档。我想将其显示为主窗口的一部分(带有工具栏、菜单和状态栏),而不是作为新窗口。
我应该使用什么控件?目前,我正在查看 FixedDocument 和 FlowDocument。我在正确的轨道上吗?有什么好的入门资料吗?
【问题讨论】:
改进上面斯蒂芬的回答...
假设您已在项目中添加了一个文档文件夹:
创建一个 GetDocument 方法,其中 GetFilePath 方法引用上述文件夹中的文件夹/文件名。
private void GetDocument()
{
string fileName = Environment.CurrentDirectory.GetFilePath("Documents\\Title.xps");
Debugger.Break();
XpsDocument doc = new XpsDocument(fileName, FileAccess.Read);
XDocViewer.Document = doc.GetFixedDocumentSequence();
}
GetFilePath 是一个扩展方法,如下所示:
public static class StringExtensions
{
public static string GetFilePath(
this string EnvironmentCurrentDirectory, string FolderAndFileName)
{
//Split on path characters
var CurrentDirectory =
EnvironmentCurrentDirectory
.Split("\\".ToArray())
.ToList();
//Get rid of bin/debug (last two folders)
var CurrentDirectoryNoBinDebugFolder =
CurrentDirectory
.Take(CurrentDirectory.Count() - 2)
.ToList();
//Convert list above to array for Join
var JoinableStringArray =
CurrentDirectoryNoBinDebugFolder.ToArray();
//Join and add folder filename passed in
var RejoinedString =
string.Join("\\", JoinableStringArray) + "\\";
var final = RejoinedString + FolderAndFileName;
return final;
}
}
【讨论】:
在 XAML 中添加文档查看器
并在cs文件中添加这段代码:
string fileName = null;
string appPath= System.IO.Path.GetDirectoryName(Assembly.GetAssembly(typeof(DocumentWindow)).CodeBase);
fileName = appPath + @"\Documents\Help.xps";
fileName = fileName.Remove(0, 6);
XpsDocument doc = new XpsDocument(fileName, FileAccess.Read);
docView.Document = doc.GetFixedDocumentSequence();
【讨论】: