private void CreateFormatTableDialog()
        {
            SetButtonState(retrieveFormatsButton, false);
            SetFormatListState(false);

            string     formatName        = (string)storedFilesList.SelectedValue;
            Connection printerConnection = null;

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

                    ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection);
                    byte[] formatData    = printer.RetrieveFormatFromPrinter(formatName);

                    fieldDescDataVars = printer.GetVariableFields(Encoding.UTF8.GetString(formatData)).ToList();
                    fieldDescDataVars = FormatFieldDescriptionDataVars(fieldDescDataVars);

                    formatVariables = new ObservableCollection <FormatVariable>();
                    for (int i = 0; i < fieldDescDataVars.Count; ++i)
                    {
                        formatVariables.Add(new FormatVariable {
                            FieldName = fieldDescDataVars[i].FieldName, FieldValue = ""
                        });
                    }

                    try {
                        if (printerConnection != null)
                        {
                            printerConnection.Close();
                        }
                    } catch (ConnectionException) { }

                    ShowStoredFormatDialog(formatName);
                } catch (Exception e) {
                    MessageBoxCreator.ShowError(e.Message, "Communication Error");
                    SetButtonState(retrieveFormatsButton, true);
                    SetFormatListState(true);
                } finally {
                    try {
                        if (printerConnection != null && printerConnection.Connected)
                        {
                            printerConnection.Close();
                        }
                    } catch (ConnectionException) { }
                }
            });
        }
示例#2
0
        private async Task PopulateVariableFieldListAsync()
        {
            await Task.Factory.StartNew(() => {
                viewModel.IsVariableFieldListRefreshing = true;

                try {
                    viewModel.FormatVariableList.Clear();
                } catch (NotImplementedException) {
                    viewModel.FormatVariableList.Clear(); // Workaround for Xamarin.Forms.Platform.WPF issue: https://github.com/xamarin/Xamarin.Forms/issues/3648
                }
            });

            Connection connection       = null;
            bool       linePrintEnabled = false;

            try {
                await Task.Factory.StartNew(() => {
                    connection = ConnectionCreator.Create(selectedPrinter);
                    connection.Open();

                    string originalPrinterLanguage = SGD.GET(DeviceLanguagesSgd, connection);
                    linePrintEnabled = "line_print".Equals(originalPrinterLanguage, StringComparison.OrdinalIgnoreCase);

                    if (linePrintEnabled)
                    {
                        SGD.SET(DeviceLanguagesSgd, "zpl", connection);
                    }

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    if (format.Source == FormatSource.Printer)
                    {
                        format.Content = Encoding.UTF8.GetString(printer.RetrieveFormatFromPrinter(format.PrinterPath));
                    }

                    FieldDescriptionData[] variableFields = printer.GetVariableFields(format.Content);
                    foreach (FieldDescriptionData variableField in variableFields)
                    {
                        viewModel.FormatVariableList.Add(new FormatVariable {
                            Name  = variableField.FieldName ?? $"Field {variableField.FieldNumber}",
                            Index = variableField.FieldNumber,
                        });
                    }
                });
            } catch (Exception e) {
                await AlertCreator.ShowErrorAsync(this, e.Message);
            } finally {
                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    try {
                        connection?.Close();
                    } catch (ConnectionException) { }

                    viewModel.IsVariableFieldListRefreshing = false;
                });
            }
        }