打印前使用 WPF 窗口作为可视化模板

Bra*_*don 5 c# wpf templates

我正在制作一个用于打印标签的 WPF 应用程序。我想设计一个标签模板作为 WPF 窗口。在另一个类中,我将实例化这个“窗口模板”,在运行时填充属性并打印标签。我无法在打印前在屏幕上显示标签,因此无法在此窗口实例上调用 .ShowDialog()。这在后面发挥作用。

过去一周我一直在做这方面的研究,我发现了两种几乎可以分别做我想做的事情的方法,如果我可以将它们结合起来就可以了,但是我遗漏了一部分。

可以让它工作,但按照这里的步骤 在 WPF 第 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");
Run Code Online (Sandbox Code Playgroud)

我只是在这里展示我是如何打印条形码的。在实际程序中,我会在标签上添加所有内容并将其定位。

在这个类中,我实例化了我的标签,填充了它的属性,并尝试将它添加到 FixedPage 的 Children - 但它返回了这个错误。

Specified element is already the logical child of another element. Disconnect it first.
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我可以从模板中删除每个 UI 元素,然后将其添加到我的固定文档中。这似乎不是最干净的解决方案,但它是可能的。

然后我想遍历此列表中的每个 UIElement,而不是手动从我的模板中删除每个元素并将其添加到我的固定文档中。如果标签需要更改,我会这样做,这会更容易。

我能够找到这个链接,它显示了如何遍历每个元素

        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;
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

然而,这似乎只适用于模板本身的代码!所以这对我想要打印的班级没有帮助。当然,我可以创建一个全局范围的变量并以这种方式传递我的 UI 元素列表,但这种方式越来越不干净。最重要的是,我只能在模板类的 Windows_Loaded 事件中调用此方法。Windows_Loaded 仅在被调用时才WindowInstance.ShowDialog();被调用,这当然会在屏幕上显示模板,这也是我不能拥有的。

无论如何要完成我正在尝试的事情,还是我最好放弃整个模板的想法并通过背后的代码定位所有内容。

更新虽然这里的一些答案为我指明了正确的方向,但要完成我想做的事情需要更多的挖掘。解决方法如下:

我在莉娜的回答的帮助下拼凑了我的解决方案,我在下面接受了这个答案和另一篇文章

要获得一个自己工作的示例,请使用 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);

        }
Run Code Online (Sandbox Code Playgroud)

len*_*ena 2

如果我误解了您的需求,我很抱歉,但我希望我的例子能给您带来一点帮助。这是完整而简单的示例 - 只需创建这些文件并测试它!

视图.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>
Run Code Online (Sandbox Code Playgroud)

查看.xaml.cs

using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class View : DockPanel
    {
        public View()
        {
            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ViewModel.cs

namespace WpfApplication1
{
    public  class ViewModel
    {
        public string Text1 { get; set; }
        public string Text2 { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用下面的代码向 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();
Run Code Online (Sandbox Code Playgroud)