与WPF C#打印 [英] C# printing with WPF

查看:224
本文介绍了与WPF C#打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application prints (to a printer) the information shown on screen (using the Canvas control) N times.

这个过程是

在用户点击一个按钮(称为打印)。结果
更新文本画布(通常从数据库但对于下面的代码,它的硬编码)结果
打印到打印机结果
更新为新文本从数据库但对于画布(再次下面的代码,它的硬编码)
打印到打印机

The user clicks a button (called Print).
Update the Canvas with text (normally from a database but for the code below, it's hard coded)
Print to printer
Update the Canvas with new text (again from a database but for the code below, it's hard coded) Print to printer

不过,我不能得到这个工作在上面解释的流程 - 打印机仅打印的最后一次更新。

However, I can't get this to work as explained in the above process- the printer only prints the last update made.

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

To make this issue replicable, I enclose the code below

我的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号

The only thing which prints is Number 2

虽然已经重复和更新,1号它永远不会打印(一个空白页打印)。

Although it has iterated and updated the canvas with Number 1 it never prints (a blank page is printed).

任何的想法是什么,我必须这样做每一油画版画?虽然我可以得到它通过展示它违背了它被自动化的目的在MessageBox工作

Any ideas what it is I need to do so each Canvas prints? Although I can get it to work by showing the messagebox it defeats the purpose of it being automated.

修改:我现在得到一个错误从我的打印机信息 - 另一台计算机正在使用的打印机。据其他网站,我要等到一个作业完成,然后第二个会自动启动,但可悲的是,它从来不会。

EDIT: I am now getting an error message from my printer - "Another computer is using the printer." According to other websites, I have to wait until one job finishes and then the second will start automatically but sadly, it never does.

推荐答案

答案(不好意思代码是不完全一样的问题,但它是如此简单,它应该很容易理解。谢谢你们每一个人。

The answer (sorry the code is not exactly the same as the question but it's so simple it should be easy to follow. Thank you every one.

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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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; }
    }
}

这篇关于与WPF C#打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆