使用MVVM的打印命令 [英] Print Command using MVVM

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

问题描述

我对wpf mvvm和C#还是很陌生.

I am pretty new to wpf mvvm and C#. 

我正在尝试在要打印视图的地方编写printcommand的功能.我已经创建了一个打印按钮,它已绑定到打印命令.单击此打印命令会创建一个打印预览窗口的实例(我已经编写了该功能 为此)在要打印其视图的视图模型中.此打印预览"窗口将PrintViewModel作为其数据上下文.我已设置属性以获取打印机,纸张尺寸,方向,要打印的页面等.

I am trying to write a functionality for printcommand where I want to print a view. I have created a print button and it is bound to a print command. This print command when clicked creates an instance of print preview window (I have written the functionality for this) in the view model whose view is to be printed. This Print preview window has PrintViewModel as its Datacontext. I have set the properties to get the printer, paper size, orientation, pages to print etc. 

我不确定如何将视图(parentviewmodel)分配给PrintPreview,以便可以预览该视图并将其打印为图像.

I am not sure how to assign the view ( of parentviewmodel) to the PrintPreview so that it can be previewed and printed as an image. 

创建的按钮如下:

在parentViewModel中的调用是:

 公共SimpleCommand PrintCommand
        {
           得到
            {
               返回printCommand ??
                    (printCommand = new SimpleCommand(ExecutePrintCommand));
            }
        }

  public SimpleCommand PrintCommand
        {
            get
            {
                return printCommand ??
                    (printCommand = new SimpleCommand(ExecutePrintCommand));
            }
        }

  public void ExecutePrintCommand(object obj)
        {

            PrintViewModel printVM =新的PrintViewModel();
            var printPreviewWindow = new UserControls.PrintPreview();
            /****printPreviewWindow.CurrentDrawing = CurrentDrawing;  //如何分配? ****/
            printPreviewWindow.ShowDialog();
            printPreviewWindow.Close();

        }

 public void ExecutePrintCommand(object obj)
        {

            PrintViewModel printVM = new PrintViewModel();
            var printPreviewWindow = new UserControls.PrintPreview();
            /****printPreviewWindow.CurrentDrawing = CurrentDrawing;  //How to assign this? ****/
            printPreviewWindow.ShowDialog();
            printPreviewWindow.Close();

        }

推荐答案

尝试以下代码:

 public partial class sample4 : Window
    {
        public sample4()
        {
            InitializeComponent();
            PrintViewModel vm = new PrintViewModel();
            vm.printSource = grid;
            this.DataContext = vm;
        }
        public class RelayCommand : ICommand
        {
            private Predicate<object> _canExecute;
            private Action<object> _execute;

            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                this._canExecute = canExecute;
                this._execute = execute;
            }

            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            public bool CanExecute(object parameter)
            {
                return _canExecute(parameter);
            }

            public void Execute(object parameter)
            {
                _execute(parameter);
            }
        }
        public class PrintViewModel
        {

           public FrameworkElement printSource { get; set; }
            private ICommand _printVisual;
            public ICommand PrintVisual
            {
                get
                {
                    if (_printVisual == null)
                    {
                        _printVisual = new RelayCommand(
                           obj => Print(printSource),
                            obj => true);
                    }
                    return _printVisual;
                }
            }

            public void Print(FrameworkElement src)
            {
                if (src != null)
                {
                    PrintDialog printDlg = new System.Windows.Controls.PrintDialog();

                    if (printDlg.ShowDialog() == true)
                    {                     
                        FixedDocument fd = PrintHelper.GetFixedDocument(src, printDlg);

                        IDocumentPaginatorSource docSource = fd;

                        PrintHelper.ShowPrintPreview(fd);

                        printDlg.PrintDocument(docSource.DocumentPaginator, "Doc");
                    }
                }
            }

            public static class PrintHelper
            {

                public static FixedDocument GetFixedDocument(FrameworkElement element, PrintDialog printDialog)
                {
                    PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
                    Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
                    Size visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
                    FixedDocument fixedDoc = new FixedDocument();
                    //If the toPrint visual is not displayed on screen we neeed to measure and arrange it   
                    element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
                    //   
                    Size size = element.DesiredSize;
                    //Will assume for simplicity the control fits horizontally on the page   
                    double yOffset = 0;
                    while (yOffset < size.Height)
                    {
                        VisualBrush vb = new VisualBrush(element);
                        vb.Stretch = Stretch.None;
                        vb.AlignmentX = AlignmentX.Left;
                        vb.AlignmentY = AlignmentY.Top;
                        vb.ViewboxUnits = BrushMappingMode.Absolute;
                        vb.TileMode = TileMode.None;
                        vb.Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height);
                        PageContent pageContent = new PageContent();
                        FixedPage page = new FixedPage();
                        ((IAddChild)pageContent).AddChild(page);
                        fixedDoc.Pages.Add(pageContent);
                        page.Width = pageSize.Width;
                        page.Height = pageSize.Height;
                        Canvas canvas = new Canvas();
                        FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
                        FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
                        canvas.Width = visibleSize.Width;
                        canvas.Height = visibleSize.Height;
                        canvas.Background = vb;
                        page.Children.Add(canvas);
                        yOffset += visibleSize.Height;
                    }
                    return fixedDoc;
                }

                public static void ShowPrintPreview(FixedDocument fixedDoc)
                {
                    Window wnd = new Window();
                    DocumentViewer viewer = new DocumentViewer();
                    viewer.Document = fixedDoc as IDocumentPaginatorSource;
                    wnd.Content = viewer;
                    wnd.ShowDialog();
                }
            }
        }
    }



最好的问候,

鲍勃


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

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