when the button is pressed to print, the experience occurs.

Javier R 211 Reputation points
2021-08-14T17:39:19.923+00:00

when the button is pressed to print, the experience occurs.123249-captura-de-pantalla-38.png
Object reference not set to an instance of an object. PrintSerive_InCanvas is never assigned and will always default to null mvvm format

this exception occurs with printcanvas drawingcanvas inkcanvas in the printservice class

  private   async void Print_Click(object sender, RoutedEventArgs e)  
        {  
             
              
               var service = new PrintService();  
  
               await  service.PrintAsync();  
  
               
                      
        }  
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2021-08-19T02:20:36.95+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Based on your code, your PrintService class can't find _inkCanvas object, _drawingCanvas object, _printCanvas object. The PrintService Class can't directly get these XAML elements. You could pass these objects from UI when you create the PrintService Class.

    Like this.

            private PointHelper _printHelper;  
            private  InkCanvas _inkCanvas;  
            private  Canvas _drawingCanvas, _printCanvas;  
      
            public PrintService(InkCanvas inkcanvas, Canvas drawingCanvas, Canvas printCanvas)  
            {  
                _inkCanvas = inkcanvas;  
                _drawingCanvas = drawingCanvas;  
                _printCanvas = printCanvas;  
            }  
    

    Then in the button click event, call it like this:

      var service = new PrintService(yourinkcanvas,yourdrawcanvas,yourprintcanvas);  
    

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Javier R 211 Reputation points
    2021-08-18T18:52:50.367+00:00
    using Microsoft.Toolkit.Uwp.Helpers;
    using System;
    using System.Threading.Tasks;
    using Windows.Graphics.Printing;
    using Windows.Storage.Streams;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media.Imaging;
    
    namespace InkPen.Helpers
    {
        public class PrintService
        {
    
    
    
            private PrintHelper _printHelper;
            private readonly InkCanvas _inkCanvas;
            private readonly Canvas _drawingCanvas, _printCanvas;
    
            public PrintService()
            {
            }
    
    
    
    
    
            public  async Task  PrintAsync()
            {
                var inkStream = new InMemoryRandomAccessStream();
                await _inkCanvas.InkPresenter.StrokeContainer.SaveAsync(inkStream.GetOutputStreamAt(0));
                var inkBitmap = new BitmapImage();
                await inkBitmap.SetSourceAsync(inkStream);
    
                var inkBounds = _inkCanvas.InkPresenter.StrokeContainer.BoundingRect;
                var inkMargin = new Thickness(inkBounds.Left, inkBounds.Top, _inkCanvas.ActualWidth - inkBounds.Right, _inkCanvas.ActualHeight - inkBounds.Bottom);
    
                var inkViewBox = new Viewbox()
                {
    
                    Child = new Image()
                    {
                        Source = inkBitmap,
                        Margin = inkMargin
                    },
    
                    Width = _drawingCanvas.ActualWidth,
                    Height = _drawingCanvas.ActualHeight
                };
    
                _printHelper = new PrintHelper(_printCanvas);
                _printHelper.AddFrameworkElementToPrint(inkViewBox);
    
                _printHelper.OnPrintFailed += printHelper_OnPrintFailed;
                _printHelper.OnPrintSucceeded += printHelper_OnPrintSucceeded;
    
                var printHelperOptions = new PrintHelperOptions();
    
                printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Copies);
                printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.MediaSize);
                printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Collation);
                printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Orientation);
    
    
                printHelperOptions.PrintQuality = PrintQuality.Default;
                printHelperOptions.MediaSize = PrintMediaSize.Default;
                printHelperOptions.Collation = PrintCollation.Default;
                printHelperOptions.Orientation = PrintOrientation.Landscape;
    
    
    
              await _printHelper.ShowPrintUIAsync("Printing Ink");
    
            }
    
    
    
            private async void printHelper_OnPrintSucceeded()
            {
                _printHelper.Dispose();
    
                var dialog = new MessageDialog("Printing Done");
                await dialog.ShowAsync();
    
            }
    
    
            private async void printHelper_OnPrintFailed()
            {
                _printHelper.Dispose();
                var dialog = new MessageDialog("Sorry, Printing Failed");
                await dialog.ShowAsync();
    
            }
    
    
        }
    }
    
    0 comments No comments