예제 #1
0
        private async void PrintButton_Clicked(object sender, EventArgs eventArgs)
        {
            await Task.Factory.StartNew(() => {
                viewModel.IsSendingPrintJob = true;
            });

            Connection connection       = null;
            bool       linePrintEnabled = false;

            try {
                await Task.Factory.StartNew(() => {
                    connection = ConnectionCreator.Create(selectedPrinter);
                    connection.Open();

                    string originalPrinterLanguage = SGD.GET(DeviceLanguagesSgd, connection);
                    linePrintEnabled = "line_print".Equals(originalPrinterLanguage, StringComparison.OrdinalIgnoreCase);

                    if (linePrintEnabled)
                    {
                        SGD.SET(DeviceLanguagesSgd, "zpl", connection);
                    }

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    string errorMessage = GetPrinterStatusErrorMessage(printer.GetCurrentStatus());
                    if (errorMessage != null)
                    {
                        throw new PrinterNotReadyException($"{errorMessage}. Please check your printer and try again.");
                    }
                    else
                    {
                        if (format.Source != FormatSource.Printer)
                        {
                            connection.Write(Encoding.UTF8.GetBytes(format.Content));
                        }

                        linkOsPrinter.PrintStoredFormatWithVarGraphics(format.PrinterPath, BuildFormatVariableDictionary(), "UTF-8");
                    }
                });

                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    viewModel.IsSendingPrintJob = false;
                });
            } catch (PrinterNotReadyException e) {
                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    viewModel.IsSendingPrintJob = false;
                });

                await AlertCreator.ShowAsync(this, "Printer Error", e.Message);
            } catch (Exception e) {
                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    viewModel.IsSendingPrintJob = false;
                });

                await AlertCreator.ShowErrorAsync(this, e.Message);
            } finally {
                await Task.Factory.StartNew(() => {
                    try {
                        connection?.Close();
                    } catch (ConnectionException) { }
                });
            }
        }