使用 WPF C# 打印

标签 c# printing dispatcher

我的应用程序将屏幕上显示的信息(使用 Canvas 控件)打印(到打印机)N 次。

流程是

用户单击一个按钮(称为“打印”)。
使用文本更新 Canvas (通常来自数据库,但对于下面的代码,它是硬编码的)
打印到打印机
使用新文本更新 Canvas (同样来自数据库,但对于下面的代码,它是硬编码的) 打印到打印机

但是,我无法按照上述过程中的说明使其工作 - 打印机仅打印最后一次更新。

为了使此问题可复制,我附上了下面的代码

我的 XAML

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Canvas Margin="0,0,0,88" Name="canvas1">
        <TextBlock Text="Hello World" Name="TextBlock1" />
    </Canvas>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="0,245,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>

和我的代码

using System;
using System.Windows;
using System.Printing;
using System.Windows.Threading;
using System.Windows.Controls;

namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog dialog = new PrintDialog();
        for (int i = 1; i < 3; i++)
        {
            //showing this message box fixes the issue
            //MessageBox.Show("01");
            updateTextblock(i);

            //use the dispatcher object to ensure all renders and databinding are completed before sending to print   
            DispatcherOperation disO;
            disO = Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(delegate
            {
                print(dialog);
            }
            ));   
            disO.Wait()
        }
    }

    private void print(PrintDialog dialog)
    {
        //select printer auotmatically
        PrintQueue queue = new LocalPrintServer().GetPrintQueue("Canon MG160 series WS");
        //assign the printer  
        dialog.PrintQueue = queue;

        dialog.PrintVisual(canvas1, "");
    }

    private void updateTextblock(int i)
    {
        TextBlock1.Text = "Number " + i.ToString();
    }
}
}    

唯一打印的是 2号

虽然它已经用 Number 1 迭代并更新了 Canvas ,但它永远不会打印(打印空白页)。

有什么想法我需要做什么才能打印每个 Canvas ?虽然我可以通过显示消息框来使其工作,但它违背了自动化的目的。

编辑:我现在从打印机收到一条错误消息 - “另一台计算机正在使用该打印机。”根据其他网站,我必须等到一项工作完成,然后第二项工作才会自动开始,但遗憾的是,它永远不会。

最佳答案

Use PrintDialog dialog = new PrintDialog(); inside for loop Use this function

 private void button1_Click(object sender, RoutedEventArgs e)
    {

        for (int i = 1; i < 3; i++)
        {
           PrintDialog dialog = new PrintDialog();
            //showing this message box fixes the issue
            //MessageBox.Show("01");
            updateTextblock(i);

            //use the dispatcher object to ensure all renders and databinding are completed before sending to print   
            Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(delegate
            {
                print(dialog);
            }
            ));   
        }
    }

关于使用 WPF C# 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10902791/

相关文章:

c# - Unity - 在游戏中添加背景音乐

python - 打印未显示在 ipython 笔记本中

c# - 使用 for 循环获取 DataTable 到元组列表

c# - 等待和预防死锁 - 澄清?

c# - 通过编码编辑 MailItem.RTFBody

c# - WPF 调度程序 {"The calling thread cannot access this object because a different thread owns it."}

c# - 在新线程上使用 ObservableCollection

Cakephp:pr() 不显示数据?

c# - WPF 将 RichTextBox 打印为图像

c# - 使用 Dispatcher 切换到 UI 线程的正确语法