使用WPF进行C#打印

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

这个过程是

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

但是,我无法按照上述过程中的说明进行操作 – 打印机仅打印上次更新。

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

我的XAML

     

和我的代码背后

 using System; using System.Windows; using System.Printing; using System.Windows.Threading; using System.Windows.Controls; namespace WpfApplication4 { ///  /// Interaction logic for MainWindow.xaml ///  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号

虽然它已经迭代并使用数字1更新了canvas,但它从不打印(打印空白页)。

任何想法我需要这样做每个Canvas打印? 虽然我可以通过显示消息框来使其工作,但它失败了它自动化的目的。

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

使用PrintDialog对话框= new PrintDialog(); inside for loop使用此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); } )); } } 

答案(对不起,代码与问题不完全相同,但它很简单,应该很容易理解。谢谢大家。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace WpfApplication1 { ///  /// Interaction logic for MainWindow.xaml ///  public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < 2; i++) { result res = null; if (i == 0) res = new result { Comments = "First Time Round" }; if (i == 1) res = new result { Comments = "Total hard type" }; Dispatcher.Invoke(DispatcherPriority.Loaded, new Action(delegate { this.DataContext = res; } )); System.Threading.Thread.Sleep(500); Dispatcher.Invoke(DispatcherPriority.Loaded, new Action(delegate { PrintDialog dialog = new PrintDialog(); dialog.PrintVisual(this.canvas1, ""); } )); System.Threading.Thread.Sleep(500); } } } class result { public string Comments { get; set; } } } 

你的UpdateText方法都错了。 每次调用方法时,都会在文本块中创建一个新字符串。 因此,您将看到的是对例程的最后一次调用。

相反,您应该附加到文本而不是替换文本。

BeginInvoke返回DispatcherOperation 。 如果在继续执行for循环之前调用Wait on,则在执行第一次打印之前,文本框不会更新。