Exemple #1
0
        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            string ipAddressText = IpAddressInput.Text;

            IndeterminateProgressDialog indeterminateProgressDialog = new IndeterminateProgressDialog($"Connecting to {ipAddressText}...");

            Task.Run(() => {
                Application.Current.Dispatcher.Invoke(() => {
                    indeterminateProgressDialog.ShowDialog();
                });
            });

            Task.Run(() => {
                TcpConnection connection          = null;
                ZebraCardPrinter zebraCardPrinter = null;

                try {
                    connection = GetTcpConnection(ipAddressText);
                    connection.Open();

                    zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

                    string model = zebraCardPrinter.GetPrinterInformation().Model;
                    if (model != null)
                    {
                        if (!model.ToLower().Contains("zxp1") && !model.ToLower().Contains("zxp3"))
                        {
                            printerManager.Printer = new DiscoveredCardPrinterNetwork(DiscoveryUtilCard.GetDiscoveryDataMap(connection));

                            Application.Current.Dispatcher.Invoke(() => {
                                Close();
                            });
                        }
                        else
                        {
                            throw new ConnectionException("Printer model not supported");
                        }
                    }
                    else
                    {
                        throw new SettingsException("No printer model found");
                    }
                } catch (Exception exception) {
                    MessageBoxHelper.ShowError($"Error connecting to printer {ipAddressText}: {exception.Message}");
                } finally {
                    ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);

                    Application.Current.Dispatcher.Invoke(() => {
                        indeterminateProgressDialog.Close();
                    });
                }
            });
        }
        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (PreviewTab.IsSelected)
            {
                viewModel.TemplatePreviews.Clear();

                IndeterminateProgressDialog indeterminateProgressDialog = new IndeterminateProgressDialog("Refreshing preview...");

                Task.Run(() => {
                    Application.Current.Dispatcher.Invoke(() => {
                        indeterminateProgressDialog.ShowDialog();
                    });
                });

                Task.Run(() => {
                    try {
                        if (!Path.IsPathRooted(viewModel.TemplateFilename))
                        {
                            throw new ArgumentException("Template filename must be an absolute path");
                        }

                        if (viewModel.ImageDirectory != null && !Path.IsPathRooted(viewModel.ImageDirectory))
                        {
                            throw new ArgumentException("Image directory must be an absolute path");
                        }

                        List <GraphicsInfo> graphicsData = CreateTemplateJob().GraphicsData;
                        foreach (GraphicsInfo graphicsInfo in graphicsData)
                        {
                            Application.Current.Dispatcher.Invoke(() => {
                                viewModel.TemplatePreviews.Add(new TemplatePreview {
                                    Label     = $"{graphicsInfo.Side} ({graphicsInfo.PrintType})",
                                    ImageData = graphicsInfo.GraphicData?.ImageData
                                });
                            });
                        }
                    } catch (Exception exception) {
                        MessageBoxHelper.ShowError($"Error getting template preview: {exception.Message}");

                        Application.Current.Dispatcher.Invoke(() => {
                            TabControl.SelectedItem = TemplateTab;
                        });
                    } finally {
                        Application.Current.Dispatcher.Invoke(() => {
                            indeterminateProgressDialog.Close();
                        });
                    }
                });
            }
        }
        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            DiscoveredPrinter printer = viewModel.SelectedPrinter;
            string            address = printer.Address;

            IndeterminateProgressDialog indeterminateProgressDialog = new IndeterminateProgressDialog($"Connecting to {address}...");

            Task.Run(() => {
                Application.Current.Dispatcher.Invoke(() => {
                    indeterminateProgressDialog.ShowDialog();
                });
            });

            Task.Run(() => {
                Connection connection             = null;
                ZebraCardPrinter zebraCardPrinter = null;

                try {
                    connection = printer.GetConnection();
                    connection.Open();

                    zebraCardPrinter       = ZebraCardPrinterFactory.GetInstance(connection);
                    printerManager.Printer = printer;

                    Application.Current.Dispatcher.Invoke(() => {
                        Close();
                    });
                } catch (Exception exception) {
                    Application.Current.Dispatcher.Invoke(() => {
                        MessageBoxHelper.ShowError($"Error connecting to printer {address}: {exception.Message}");
                    });
                } finally {
                    ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);

                    Application.Current.Dispatcher.Invoke(() => {
                        indeterminateProgressDialog.Close();
                    });
                }
            });
        }