Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.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# 将WPF(XAML)控件转换为XPS文档_C#_Wpf_Xaml_Xps_Xpsviewer - Fatal编程技术网

C# 将WPF(XAML)控件转换为XPS文档

C# 将WPF(XAML)控件转换为XPS文档,c#,wpf,xaml,xps,xpsviewer,C#,Wpf,Xaml,Xps,Xpsviewer,我是否可以获取现有的WPF(XAML)控件,对其进行数据绑定,并将其转换为可以使用WPF XPS文档查看器显示和打印的XPS文档? 如果是,怎么做? 如果没有,我应该如何使用XPS/PDF/etc在WPF中进行“报告” 基本上,我想获取一个现有的WPF控件,对其进行数据绑定以将有用的数据放入其中,然后使其可打印并可保存给最终用户。理想情况下,文档创建将在内存中完成,除非用户专门保存文档,否则不会命中磁盘。这可行吗?事实上,在处理了大量不同的样本后,我发现Eric Sinks的文章是关于 简化的代

我是否可以获取现有的WPF(XAML)控件,对其进行数据绑定,并将其转换为可以使用WPF XPS文档查看器显示和打印的XPS文档? 如果是,怎么做? 如果没有,我应该如何使用XPS/PDF/etc在WPF中进行“报告”


基本上,我想获取一个现有的WPF控件,对其进行数据绑定以将有用的数据放入其中,然后使其可打印并可保存给最终用户。理想情况下,文档创建将在内存中完成,除非用户专门保存文档,否则不会命中磁盘。这可行吗?

事实上,在处理了大量不同的样本后,我发现Eric Sinks的文章是关于
简化的代码只有10行长

public void CreateMyWPFControlReport(MyWPFControlDataSource usefulData)
{
  //Set up the WPF Control to be printed
  MyWPFControl controlToPrint;
  controlToPrint = new MyWPFControl();
  controlToPrint.DataContext = usefulData;

  FixedDocument fixedDoc = new FixedDocument();
  PageContent pageContent = new PageContent();
  FixedPage fixedPage = new FixedPage();

  //Create first page of document
  fixedPage.Children.Add(controlToPrint);
  ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
  fixedDoc.Pages.Add(pageContent);
  //Create any other required pages here

  //View the document
  documentViewer1.Document = fixedDoc;
}
我的示例是相当简单的,它不包括页面大小和方向,其中包含一组完全不同的问题,而这些问题并不像您所期望的那样有效。 它也不包含任何保存功能,因为MS似乎忘记在文档查看器中包含保存按钮

保存功能相对简单(这也是本文的内容)

public void SaveCurrentDocument()
{
//“配置保存文件”对话框
Microsoft.Win32.SaveFileDialog dlg=新建Microsoft.Win32.SaveFileDialog();
dlg.FileName=“MyReport”;//默认文件名
dlg.DefaultExt=“.xps”//默认文件扩展名
dlg.Filter=“XPS文档(.XPS)|*.XPS”//按扩展名筛选文件
//显示保存文件对话框
可为空的结果=dlg.ShowDialog();
//“处理保存文件”对话框结果
如果(结果==真)
{
//保存文档
字符串文件名=dlg.filename;
FixedDocument文档=(FixedDocument)文档查看器1.Document;
XpsDocument xpsd=新的XpsDocument(文件名,FileAccess.ReadWrite);
System.Windows.Xps.XpsDocumentWriter xw=XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.写入(doc);
xpsd.Close();
}
}

因此,答案是肯定的,您可以使用现有的WPF(XAML)控件,对其进行数据绑定并将其转换为XPS文档,这并不困难。

您可以提供MyWPFControl和MyWPFControlDataSource的定义吗???示例代码没有它们就一文不值,而Sinks文章似乎也不包含它们。Jim,MyWPFControl是您希望在XPS文档中呈现为页面的任何控件(自定义、复合、独立或其他)。MyWPFControlDataSource显然是要绑定到该控件的任何数据(通常是ViewModel)。该示例故意保留为通用形式,因此它对任何查看它的人都很有用,而不仅仅是希望将特定控件呈现给xaml的人。这是一个幸运的日子,在你的简单答案中找到了我本周两个问题的解决方案。这是与大型WPF控件一起工作,还是我需要为多页添加一些额外的代码?@Scott,我正在寻找一种方法来创建类似的内容,但使用自动分页(无需创建页面并手动将其添加到
FixedDocument
),有办法吗?
public void SaveCurrentDocument()
{
 // Configure save file dialog box
 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
 dlg.FileName = "MyReport"; // Default file name
 dlg.DefaultExt = ".xps"; // Default file extension
 dlg.Filter = "XPS Documents (.xps)|*.xps"; // Filter files by extension

 // Show save file dialog box
 Nullable<bool> result = dlg.ShowDialog();

 // Process save file dialog box results
 if (result == true)
 {
   // Save document
   string filename = dlg.FileName;

  FixedDocument doc = (FixedDocument)documentViewer1.Document;
  XpsDocument xpsd = new XpsDocument(filename, FileAccess.ReadWrite);
  System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
  xw.Write(doc);
  xpsd.Close();
 }
}