private async void PrintButton_Clicked(object sender, EventArgs eventArgs)
        {
            SetInputEnabled(false);

            Connection connection = null;

            try {
                await Task.Factory.StartNew(() => {
                    connection = CreateConnection();
                    connection.Open();

                    ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);

                    if (printer.PrinterControlLanguage == PrinterLanguage.LINE_PRINT)
                    {
                        throw new ConnectionException("Operation cannot be performed with a printer set to line print mode");
                    }

                    string signaturePath = Path.Combine(LocalApplicationDataFolderPath, "signature.jpeg");
                    double printWidth    = double.Parse(SGD.GET("ezpl.print_width", connection));

                    if (File.Exists(signaturePath))
                    {
                        File.Delete(signaturePath);
                    }

                    using (Stream stream = SignaturePad.GetImageStream(printWidth)) {
                        using (BinaryReader br = new BinaryReader(stream)) {
                            br.BaseStream.Position = 0;
                            byte[] signatureBytes  = br.ReadBytes((int)stream.Length);
                            File.WriteAllBytes(signaturePath, signatureBytes);
                        }
                    }

                    ZebraImageI image = ZebraImageFactory.Current.GetImage(signaturePath);
                    printer.PrintImage(image, 0, 0, image.Width, image.Height, false);
                });
            } catch (Exception e) {
                await DisplayAlert("Error", e.Message, "OK");
            } finally {
                try {
                    connection?.Close();
                } catch (ConnectionException) { }

                SetInputEnabled(true);
            }
        }
Пример #2
0
        private void PrintButton_Click(object sender, RoutedEventArgs evt)
        {
            SetPrintButtonState(false);
            Connection printerConnection = null;

            Task.Run(() => {
                try {
                    printerConnection = connectionSelector.GetConnection();
                    printerConnection.Open();
                    ZebraImageI image = ZebraImageFactory.GetImage(fileSelector.FileName);
                    if (viewModel.ShouldStoreImage)
                    {
                        ZebraPrinterFactory.GetInstance(printerConnection).StoreImage(viewModel.StoredFileName, image, 540, 412);
                    }
                    ZebraPrinterFactory.GetInstance(printerConnection).PrintImage(image, 0, 0, 550, 412, false);
                } catch (ConnectionException e) {
                    MessageBoxCreator.ShowError(e.Message, "Connection Error");
                } catch (ZebraPrinterLanguageUnknownException e) {
                    MessageBoxCreator.ShowError(e.Message, "Connection Error");
                } catch (IOException e) {
                    MessageBoxCreator.ShowError(e.Message, "Image Error");
                }  catch (ZebraIllegalArgumentException e) {
                    MessageBoxCreator.ShowError(e.Message, "Illegal Arguments");
                } catch (ArgumentException e) {
                    MessageBoxCreator.ShowError(e.Message, "Invalid File Path");
                } finally {
                    try {
                        if (printerConnection != null)
                        {
                            printerConnection.Close();
                        }
                    } catch (ConnectionException) {
                    } finally {
                        SetPrintButtonState(true);
                    }
                }
            });
        }