private void CreateProfile(string profilePath, Connection connection)
        {
            if (connection != null)
            {
                try {
                    connection.Open();
                    ZebraPrinter       genericPrinter = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs printer        = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter);

                    if (printer != null)
                    {
                        if (!profilePath.EndsWith(".zprofile"))
                        {
                            profilePath += ".zprofile";
                        }
                        printer.CreateProfile(profilePath);
                        MessageBoxCreator.ShowInformation($"Profile created successfully at location '{profilePath}'", "Profile Created Successfully");
                    }
                    else
                    {
                        MessageBoxCreator.ShowError("Profile creation 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 (ZebraIllegalArgumentException e) {
                    MessageBoxCreator.ShowError(e.Message, "Connection Error");
                } catch (Exception e) {
                    MessageBoxCreator.ShowError(e.Message, "Create Profile Error");
                } finally {
                    SetButtonStates(true);
                    try {
                        connection.Close();
                    } catch (ConnectionException) { }
                }
            }
        }
Example #2
0
        private async void CreateProfileButton_Clicked(object sender, EventArgs eventArgs)
        {
            SetInputEnabled(false);

            string     filename         = FilenameEntry.Text;
            Connection connection       = null;
            bool       linePrintEnabled = false;

            try {
                if (!string.IsNullOrWhiteSpace(filename))
                {
                    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);
                        }

                        if (linkOsPrinter != null)
                        {
                            if (!filename.EndsWith(".zprofile"))
                            {
                                filename += ".zprofile";
                            }
                            string path = Path.Combine(LocalApplicationDataFolderPath, filename);
                            linkOsPrinter.CreateProfile(path);

                            Xamarin.Forms.Device.BeginInvokeOnMainThread(async() => {
                                await DisplayAlert("Profile Created Successfully", $"Profile created successfully with filename {filename}", "OK");
                            });

                            RefreshLocalApplicationDataFiles();
                        }
                        else
                        {
                            Xamarin.Forms.Device.BeginInvokeOnMainThread(async() => {
                                await DisplayAlert("Error", "Profile creation is only available on Link-OS\u2122 printers", "OK");
                            });
                        }
                    });
                }
                else
                {
                    await DisplayAlert("Invalid Filename", "Please enter a valid filename", "OK");

                    SetInputEnabled(true);
                }
            } 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);
            }
        }