private void PerformListFiles(bool isFormat)
        {
            Connection printerConnection = null;

            try {
                Application.Current.Dispatcher.Invoke(() => {
                    printerConnection = connectionSelector.GetConnection();
                });

                printerConnection.Open();
                ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection);

                string[] formatExtensions;
                if (printer.PrinterControlLanguage == PrinterLanguage.ZPL)
                {
                    formatExtensions = new string[] { "ZPL" };
                }
                else
                {
                    formatExtensions = new string[] { "FMT", "LBL" };
                }

                string[] formats = isFormat ? printer.RetrieveFileNames(formatExtensions) : printer.RetrieveFileNames();

                Application.Current.Dispatcher.Invoke(() => {
                    foreach (string format in formats)
                    {
                        viewModel.FormatsList.Add(format);
                    }
                });
            } catch (ConnectionException e) {
                MessageBoxCreator.ShowError(e.Message, "Connection Error");
            } catch (ZebraIllegalArgumentException e) {
                MessageBoxCreator.ShowError(e.Message, "Connection Error");
            } catch (ZebraPrinterLanguageUnknownException e) {
                MessageBoxCreator.ShowError(e.Message, "Connection Error");
            } finally {
                if (printerConnection != null)
                {
                    printerConnection.Close();
                }
            }
        }
        private void PopulateListWithFormats()
        {
            Connection printerConnection = null;

            Task.Run(() => {
                try {
                    ClearFormatList();
                    printerConnection = connectionSelector.GetConnection();

                    printerConnection.Open();
                    ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection);

                    string[] formatExtensions = printer.PrinterControlLanguage == PrinterLanguage.ZPL ? new String[] { "ZPL" } : new String[] { "FMT", "LBL" };
                    string[] formats          = printer.RetrieveFileNames(formatExtensions);

                    AddFormatsToList(formats);
                } catch (ConnectionException e) {
                    MessageBoxCreator.ShowError(e.Message, "Communication Error");
                } catch (ZebraPrinterLanguageUnknownException e) {
                    MessageBoxCreator.ShowError(e.Message, "Communication Error");
                } catch (ZebraIllegalArgumentException e) {
                    MessageBoxCreator.ShowError(e.Message, "Communication Error");
                } finally {
                    try {
                        if (printerConnection != null)
                        {
                            printerConnection.Close();
                        }
                    } catch (ConnectionException) {
                    } finally {
                        SetButtonState(retrieveFormatsButton, true);
                        SetFormatListState(true);
                    }
                }
            });
        }
コード例 #3
0
        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) { }
                });
            }
        }