private async void CheckPrinterStatusButton_Clicked(object sender, EventArgs eventArgs) { SetInputEnabled(false); PrinterStatusLabel.Text = "Checking printer status..."; Connection connection = null; try { await Task.Factory.StartNew(() => { connection = CreateConnection(); connection.Open(); ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection); ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); PrinterStatus printerStatus = linkOsPrinter?.GetCurrentStatus() ?? printer.GetCurrentStatus(); Device.BeginInvokeOnMainThread(() => { PrinterStatusLabel.Text = BuildPrinterStatusString(printerStatus); }); }); } catch (Exception e) { PrinterStatusLabel.Text = $"Error: {e.Message}"; await DisplayAlert("Error", e.Message, "OK"); } finally { try { connection?.Close(); } catch (ConnectionException) { } SetInputEnabled(true); } }
private void GetPrinterStatus() { Connection printerConnection = null; Task.Run(() => { try { printerConnection = connectionSelector.GetConnection(); printerConnection.Open(); ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection); ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); ZebraPrinterStatus status = (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus(); string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage(); List <string> printerStatusPrefix = GetPrinterStatusPrefix(status); StringBuilder sb = new StringBuilder(); foreach (string s in printerStatusPrefix) { sb.AppendLine(s); } foreach (string s in printerStatusString) { sb.AppendLine(s); } Application.Current.Dispatcher.Invoke(() => { printerStatus.Text = sb.ToString(); }); } catch (ConnectionException e) { MessageBoxCreator.ShowError(e.Message, "Connection Error"); } catch (ZebraPrinterLanguageUnknownException e) { MessageBoxCreator.ShowError(e.Message, "Connection Error"); } finally { if (printerConnection != null) { try { printerConnection.Close(); } catch (ConnectionException) { } finally { SetTestButtonState(true); } } else { SetTestButtonState(true); } } }); }
private void TestConnectionString() { Task.Run(() => { try { ClearProgress(); connection = ZebraConnectionBuilder.Build(GetConnectionStringForSdk()); PublishProgress("Connection string evaluated as class type " + connection.GetType().Name); connection.Open(); PublishProgress("Connection opened successfully"); if (IsAttemptingStatusConnection()) { ZebraPrinterLinkOs printer = ZebraPrinterFactory.GetLinkOsPrinter(connection); PublishProgress("Created a printer, attempting to retrieve status"); ZebraPrinterStatus status = printer.GetCurrentStatus(); PublishProgress("Is printer ready to print? " + status.isReadyToPrint); } else { ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection); PublishProgress("Created a printer, attempting to print a config label"); printer.PrintConfigurationLabel(); } PublishProgress("Closing connection"); } catch (ConnectionException) { MessageBoxCreator.ShowError("Connection could not be opened", "Error"); } catch (ZebraPrinterLanguageUnknownException) { MessageBoxCreator.ShowError("Could not create printer", "Error"); } finally { if (connection != null) { try { connection.Close(); } catch (ConnectionException) { } finally { connection = null; SetTestButtonState(true); } } else { SetTestButtonState(true); } } }); }
/// <summary> /// 获取打印机状态 /// </summary> /// <returns></returns> public PrinterStatus GetPrinterStatus() { lock (connectionHelper) { connection = null; } lock (printerHelper) { printer = null; } PrinterStatus printerStatus = null; if (TryOpenPrinterConnection()) { printer = GetPrinter(); if (printer != null) { try { ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); printerStatus = (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus(); } catch (Exception ex) { myEventLog.LogError("获取打印机状态出错!", ex); } finally { } } } return(printerStatus); }