C# printing with WPF

前端 未结 4 1767
囚心锁ツ
囚心锁ツ 2021-01-15 04:30

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

The process is

The user clicks a button (called Pri

相关标签:
4条回答
  • 2021-01-15 05:02

    Your UpdateText method is all wrong. Each time you call the method you are creating a new string in the textblock. So as a result all you will see is the last call to the routine.

    Instead you should be appending to the text instead of replacing the text.

    0 讨论(0)
  • 2021-01-15 05:06

    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; }
        }
    }
    
    0 讨论(0)
  • 2021-01-15 05:20

    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);
                }
                ));   
            }
        }
    
    0 讨论(0)
  • 2021-01-15 05:27

    The BeginInvoke returns a DispatcherOperation. If you call Wait on it before continuing in the for loop, the textbox will not be updated, until the first print has been performed.

    0 讨论(0)
提交回复
热议问题