Universal Windows Platform (UWP)
A Microsoft platform for building and publishing apps for Windows desktop devices.
2,523 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello:
as I can print in mvv m with inkcanvas ? I have this code and I get a failure Object reference not set to an instance of an object.'
this code the PrinterHelper.cs
{
public static PrintHelper _printHelper;
private readonly InkCanvas _inkCanvas;
private readonly Canvas _printCanvas;
public PrinterHelper (InkCanvas inkCanvas,Canvas printCanvas)
{
_inkCanvas = inkCanvas;
_printCanvas = printCanvas ;
}
//Craete Bitmap from strokes
public async Task PrintHelper()
{
var inkStream = new InMemoryRandomAccessStream();
await _inkCanvas.InkPresenter.StrokeContainer.SaveAsync(inkStream.GetOutputStreamAt(0));
var inkBitmap = new BitmapImage();
await inkBitmap.SetSourceAsync(inkStream);
//you need to adjust to layout the image properly in the print-page.
var inkBounds = _inkCanvas.InkPresenter.StrokeContainer.BoundingRect;
var inkMargin = new Thickness(inkBounds.Left, inkBounds.Top, _inkCanvas.ActualWidth - inkBounds.Right, _inkCanvas.ActualHeight - inkBounds.Bottom);
//prepare Viebox + image to be printed.
var inkViewBox = new Viewbox()
{
Child = new Image()
{
Source = inkBitmap,
Margin = inkMargin
},
Width = _inkCanvas.ActualWidth,
Height = _inkCanvas.ActualHeight
};
_printHelper = new PrintHelper(_printCanvas);
_printHelper.AddFrameworkElementToPrint(inkViewBox);
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.Collation = PrintCollation.Default;
printHelperOptions.Orientation = PrintOrientation.Landscape;
}
private async void PrintHelper_OnPrintSucceded()
{
_printHelper.Dispose();
var dialog = new MessageDialog("Printing Done");
await dialog.ShowAsync();
}
private async void PrintHelper_OnPrintFailed()
{
_printHelper.Dispose();
var dialog = new MessageDialog("Printing Failed");
await dialog.ShowAsync();
}
public static async Task ShowPrintUIAsync()
{
var printHelperOptions = new PrintHelperOptions();
await _printHelper.ShowPrintUIAsync("Printing INKPen", printHelperOptions);
}
}
}
and buttton private async void PrintButton_Click(object sender, RoutedEventArgs e) { await Components.PrinterHelper.ShowPrintUIAsync(); }
Can you view the stack information? When an error occurs, select
View Details
, open$exception
, and selectStackTrace
. The main purpose of this step is to discover what variables are null. A null value generally means that no element was created or assigned a value. You can check where the problem is based on the corresponding information.Hello:
The global variable can be used in the document PritHelper.cs As would the global variable PrintHelperOptions
Global is also a scope, as long as it is raised to a scope accessible everywhere. For example, a static variable
_printHelperOptions
is defined inApp.xaml.cs
, which can then be accessed throughApp._printHelperOptions
. But make sure it is initialized before useHello, if you have other questions, please feel free to ask
Sign in to comment