private async Task <string> RetrieveFormatContentAsync(FormatViewModel format) { string formatContent = null; Connection connection = null; try { await Task.Factory.StartNew(() => { connection = ConnectionCreator.Create(ViewModel.SelectedPrinter); connection.Open(); ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection); ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); formatContent = Encoding.UTF8.GetString(printer.RetrieveFormatFromPrinter(format.PrinterPath)); }); } catch (Exception e) { await AlertCreator.ShowErrorAsync(this, e.Message); } finally { await Task.Factory.StartNew(() => { try { connection?.Close(); } catch (ConnectionException) { } }); } return(formatContent); }
private async Task AddStoredFormatsToFormatListAsync() { List <FormatViewModel> formats = new List <FormatViewModel>(); await Task.Factory.StartNew(async() => { List <PrintStationDatabase.StoredFormat> storedFormats = await App.Database.GetStoredFormatsAsync(); foreach (PrintStationDatabase.StoredFormat storedFormat in storedFormats) { if (string.Equals(storedFormat.Extension, "zpl", StringComparison.OrdinalIgnoreCase)) { formats.Add(new FormatViewModel { DatabaseId = storedFormat.Id, DriveLetter = storedFormat.DriveLetter, Name = storedFormat.Name, Extension = storedFormat.Extension, Content = storedFormat.Content, Source = FormatSource.LocalStorage, OnDeleteButtonClicked = new Command(async(object parameter) => { if (!ViewModel.IsPrinterFormatListRefreshing && !ViewModel.IsSavingFormat) { FormatViewModel format = parameter as FormatViewModel; bool deleteFormat = await DisplayAlert("Delete Format", $"Are you sure you want to delete the stored format '{format.PrinterPath}'?", "Delete", "Cancel"); if (deleteFormat) { await Task.Factory.StartNew(() => { ViewModel.IsDeletingFormat = true; format.IsDeleting = true; }); await App.Database.DeleteStoredFormatByIdAsync(format.DatabaseId); await Task.Factory.StartNew(() => { format.IsDeleting = false; ViewModel.IsDeletingFormat = false; }); await RefreshStoredFormatListAsync(); } } }), OnPrintButtonClicked = new Command(async(object parameter) => { if (!ViewModel.IsPrinterFormatListRefreshing && !ViewModel.IsSavingFormat) { FormatViewModel format = parameter as FormatViewModel; await PushPageAsync(new PrintFormatPage(ViewModel.SelectedPrinter, format)); } }) }); } } formats.ForEach(ViewModel.StoredFormatList.Add); }); }
public PrintFormatPage(DiscoveredPrinter selectedPrinter, FormatViewModel format) { InitializeComponent(); this.selectedPrinter = selectedPrinter; this.format = format; BindingContext = viewModel; Device.BeginInvokeOnMainThread(async() => { await PopulateVariableFieldListAsync(); }); }
private async Task AddPrinterFormatsToFormatListAsync() { Connection connection = null; try { List <FormatViewModel> formats = new List <FormatViewModel>(); await Task.Factory.StartNew(() => { connection = ConnectionCreator.Create(ViewModel.SelectedPrinter); connection.Open(); ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection); ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); string[] formatPrinterPaths = printer.RetrieveFileNames(new string[] { "ZPL" }); foreach (string formatPrinterPath in formatPrinterPaths) { int colonIndex = formatPrinterPath.IndexOf(":"); string driveLetter = formatPrinterPath.Substring(0, colonIndex); string name = formatPrinterPath.Substring(colonIndex + 1, formatPrinterPath.LastIndexOf(".") - colonIndex - 1); string extension = formatPrinterPath.Substring(formatPrinterPath.LastIndexOf(".") + 1); if (!string.Equals(driveLetter, "Z", StringComparison.OrdinalIgnoreCase)) // Ignore all formats stored on printer's Z drive { formats.Add(new FormatViewModel { DriveLetter = driveLetter, Name = name, Extension = extension, Content = null, // Populate this later, upon navigating to print format page Source = FormatSource.Printer, OnSaveButtonClicked = new Command(async(object parameter) => { if (!ViewModel.IsSavingFormat && !ViewModel.IsDeletingFormat && !ViewModel.IsStoredFormatListRefreshing) { FormatViewModel format = parameter as FormatViewModel; await Task.Factory.StartNew(() => { ViewModel.IsSavingFormat = true; format.IsSaving = true; }); await SavePrinterFormatAsync(format); await Task.Factory.StartNew(() => { format.IsSaving = false; ViewModel.IsSavingFormat = false; }); await RefreshStoredFormatListAsync(); } }), OnPrintButtonClicked = new Command(async(object parameter) => { if (!ViewModel.IsSavingFormat) { FormatViewModel format = parameter as FormatViewModel; await PushPageAsync(new PrintFormatPage(ViewModel.SelectedPrinter, format)); } }) }); } } formats.ForEach(ViewModel.PrinterFormatList.Add); }); } catch (Exception e) { await AlertCreator.ShowErrorAsync(this, e.Message); } finally { await Task.Factory.StartNew(() => { try { connection?.Close(); } catch (ConnectionException) { } }); } }
private async Task SavePrinterFormatAsync(FormatViewModel format) { PrintStationDatabase.StoredFormat storedFormat = null; Connection connection = null; bool fileWriteAttempted = false; bool linePrintEnabled = false; try { await Task.Factory.StartNew(async() => { connection = ConnectionCreator.Create(ViewModel.SelectedPrinter); connection.Open(); string originalPrinterLanguage = SGD.GET(PrintFormatPage.DeviceLanguagesSgd, connection); linePrintEnabled = "line_print".Equals(originalPrinterLanguage, StringComparison.OrdinalIgnoreCase); if (linePrintEnabled) { SGD.SET(PrintFormatPage.DeviceLanguagesSgd, "zpl", connection); } ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection); ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); string formatContent = Encoding.UTF8.GetString(printer.RetrieveFormatFromPrinter(format.PrinterPath)); fileWriteAttempted = true; storedFormat = new PrintStationDatabase.StoredFormat { DriveLetter = format.DriveLetter, Name = format.Name, Extension = format.Extension, Content = formatContent }; await App.Database.SaveStoredFormatAsync(storedFormat); }); } catch (Exception e) { await Task.Factory.StartNew(async() => { if (fileWriteAttempted && storedFormat != null) { await App.Database.DeleteStoredFormatByIdAsync(storedFormat.Id); } }); await AlertCreator.ShowErrorAsync(this, e.Message); } finally { if (linePrintEnabled) { await Task.Factory.StartNew(() => { try { connection?.Open(); SGD.SET(PrintFormatPage.DeviceLanguagesSgd, "line_print", connection); } catch (ConnectionException) { } }); } await Task.Factory.StartNew(() => { try { connection?.Close(); } catch (ConnectionException) { } }); } }