c# – 在打印前使用WPF窗口作为可视模板
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 在打印前使用WPF窗口作为可视模板,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6328字,纯文字阅读大概需要10分钟。
内容图文
![c# – 在打印前使用WPF窗口作为可视模板](/upload/InfoBanner/zyjiaocheng/810/28c3abb3e389489ca44e9c180037d055.jpg)
我正在制作一个用于打印标签的WPF应用程序.我想将标签模板设计为WPF窗口.在另一个类中,我将实例化这个“窗口模板”,在运行时填充属性并打印标签.我无法在打印前在屏幕上显示标签,因此我无法在此窗口实例上调用.ShowDialog().这会在以后发挥作用.
我过去一周一直在研究这个问题,我找到了两种方法几乎可以单独做我想要的东西,如果我可以将它们结合起来它会起作用,但我错过了一块.
我可以让这个工作,但按照这里的步骤
Printing in WPF Part 2
这实现了打印文档的基础知识.但是,我无法使用我的模板,我必须将代码中的所有内容放在后面.这是一个可行的选择,但我想更多地探索模板的想法.
PrintDialog pd = new PrintDialog();
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new System.Windows.Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
FixedPage page1 = new FixedPage();
page1.Width = document.DocumentPaginator.PageSize.Width;
page1.Height = document.DocumentPaginator.PageSize.Height;
// add some text to the page
Label _lblBarcode = new Label();
_lblBarcode.Content = BarcodeConverter128.StringToBarcode(palletID);
_lblBarcode.FontFamily = new System.Windows.Media.FontFamily("Code 128");
_lblBarcode.FontSize = 40;
_lblBarcode.Margin = new Thickness(96);
page1.Children.Add(_lblBarcode);
// add the page to the document
PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(page1);
document.Pages.Add(page1Content);
pd.PrintQueue = new System.Printing.PrintQueue(new System.Printing.PrintServer(), "CutePDF Writer");
pd.PrintDocument(document.DocumentPaginator, "PalletID");
我只是在这里展示我如何打印条形码.在实际程序中,我会在标签上添加所有内容并定位它.
在这个类中,我实例化我的标签,填充其属性,并尝试将其添加到FixedPage的子项 – 但它返回此错误.
Specified element is already the logical child of another element. Disconnect it first.
为了解决这个问题,我可以从模板中删除每个UI元素,然后将其添加到我的固定文档中.这似乎不是最干净的解决方案,但它是可能的.
然后,我想遍历此列表中的每个UIElement,而不是手动从模板中删除每个元素并将其添加到我的固定文档中.如果标签需要更改,我会这样做,这会使这更容易.
我能够找到显示how to iterate through each element的链接
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
但是,这似乎只适用于模板本身的代码!所以这对我的班级没有帮助,我想要打印.当然,我可以创建一个全局范围的变量并以这种方式传递我的UIelements列表,但这种方式变得越来越不干净.最重要的是,我只能在模板类的Windows_Loaded事件中调用此方法. Windows_Loaded仅在WindowInstance.ShowDialog()时被调用;被调用,当然在屏幕上显示模板,这也是我不能拥有的.
反正有没有完成我正在尝试的东西,或者我更好地废弃整个模板的想法,并通过后面的代码定位一切.
更新虽然这里的一些答案指出了我正确的方向,但我需要做更多的挖掘才能做我想做的事情.解决方案如下:
我在Lena的答案的帮助下拼凑了我的解决方案,我在下面和another post接受了这个答案
为了得到一个这样的工作示例,请使用Lena的View.xaml,View.xaml.cs,ViewModel.cs,然后使用我自己的方法打印View.xaml
private void StackOverFlow()
{
string palletID = "00801004018000020631";
PrintDialog pd = new PrintDialog();
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedDoc.DocumentPaginator.PageSize = new System.Windows.Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
fixedPage.Width = fixedDoc.DocumentPaginator.PageSize.Width;
fixedPage.Height = fixedDoc.DocumentPaginator.PageSize.Height;
fixedPage.Width = 4.0 * 96;
fixedPage.Height = 3.0 * 96;
var pageSize = new System.Windows.Size(4.0 * 96.0, 3.0 * 96.0);
View v = new View();
ViewModel vm = new ViewModel(); //This would be the label object with
//set all ViewModel.cs Props here
vm.Text1 = "MyText1";
vm.Text2 = "MyText2";
v.DataContext = vm;
v.Height = pageSize.Height;
v.Width = pageSize.Width;
v.UpdateLayout();
fixedPage.Children.Add(v);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
//Use the XpsDocumentWriter to "Write" to a specific Printers Queue to Print the document
XpsDocumentWriter dw1 = PrintQueue.CreateXpsDocumentWriter(new System.Printing.PrintQueue(new System.Printing.PrintServer(), "CutePDF Writer"));
dw1.Write(fixedDoc);
}
解决方法:
如果我误解了你的需要,我很抱歉,但我希望我的例子可以帮助你一点点.
这是完整而简单的示例 – 只需创建这些文件并进行测试!
View.xaml
<DockPanel x:Class="WpfApplication1.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="300" Width="400">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="10" Text="Static Text"/>
<Button Margin="10">Any control can be here</Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="10" Width="100" Text="{Binding Text1}"/>
<TextBox Width="100" Text="{Binding Text2}"/>
</StackPanel>
</StackPanel>
</DockPanel>
View.xaml.cs
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class View : DockPanel
{
public View()
{
InitializeComponent();
}
}
}
ViewModel.cs
namespace WpfApplication1
{
public class ViewModel
{
public string Text1 { get; set; }
public string Text2 { get; set; }
}
}
使用下面的代码用数据填充ViewModel并将其打印到文件
var path = "newdoc.xps";
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedPage.Width = 11.69 * 96;
fixedPage.Height = 8.27 * 96;
var pageSize = new System.Windows.Size(11.0 * 96.0, 8.5 * 96.0);
View v = new View();
ViewModel vm = new ViewModel();
vm.Text1 = "MyText1";
vm.Text2 = "MyText2";
v.DataContext = vm;
v.UpdateLayout();
v.Height = pageSize.Height;
v.Width = pageSize.Width;
v.UpdateLayout();
fixedPage.Children.Add(v);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
if (File.Exists(path))
File.Delete(path);
XpsDocument xpsd = new XpsDocument(path, FileAccess.ReadWrite);
XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(fixedDoc);
xpsd.Close();
内容总结
以上是互联网集市为您收集整理的c# – 在打印前使用WPF窗口作为可视模板全部内容,希望文章能够帮你解决c# – 在打印前使用WPF窗口作为可视模板所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。