private void UploadProfile(string profilePath, Connection connection) { if (connection != null) { try { connection.Open(); ZebraPrinter genericPrinter = ZebraPrinterFactory.GetInstance(connection); ZebraPrinterLinkOs printer = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter); if (printer != null) { printer.LoadProfile(profilePath, FileDeletionOption.NONE, false); string printerAddress = connectionSelector.getConnectionAddress(); MessageBoxCreator.ShowInformation($"Profile loaded successfully to printer {printerAddress}", "Profile Uploaded Successfully"); } else { MessageBoxCreator.ShowError("Profile loading is only available on Link-OS(TM) printers", "Error"); } } 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, "Connection Error"); } catch (Exception e) { MessageBoxCreator.ShowError(e.Message, "Upload Profile Error"); } finally { SetButtonStates(true); try { connection.Close(); } catch (ConnectionException) { } } } }
private async void UploadProfileButton_Clicked(object sender, EventArgs eventArgs) { SetInputEnabled(false); Connection connection = null; bool linePrintEnabled = false; try { await Task.Factory.StartNew(() => { connection = CreateConnection(); connection.Open(); ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection); ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); string originalPrinterLanguage = SGD.GET(DeviceLanguagesSgd, connection); linePrintEnabled = "line_print".Equals(originalPrinterLanguage, StringComparison.OrdinalIgnoreCase); if (linePrintEnabled) { SGD.SET(DeviceLanguagesSgd, "zpl", connection); printer = ZebraPrinterFactory.GetInstance(connection); linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); } string selectedFilename = (string)FileListView.SelectedItem; if (selectedFilename != null) { if (linkOsPrinter != null) { string path = Path.Combine(LocalApplicationDataFolderPath, selectedFilename); linkOsPrinter.LoadProfile(path, FileDeletionOption.NONE, false); Xamarin.Forms.Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Profile Uploaded Successfully", $"Profile loaded successfully to printer", "OK"); }); } else { Xamarin.Forms.Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Error", "Profile loading is only available on Link-OS\u2122 printers", "OK"); }); } } else { Xamarin.Forms.Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("No Profile Selected", "Please select a profile to upload", "OK"); }); } }); } catch (Exception e) { await DisplayAlert("Error", e.Message, "OK"); } finally { if (linePrintEnabled) { await Task.Factory.StartNew(() => { try { connection?.Open(); SGD.SET(DeviceLanguagesSgd, "line_print", connection); } catch (ConnectionException) { } }); } try { connection?.Close(); } catch (ConnectionException) { } SetInputEnabled(true); } }