ProfileView.xaml.cs
6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***********************************************
* CONFIDENTIAL AND PROPRIETARY
*
* The source code and other information contained herein is the confidential and exclusive property of
* ZIH Corp. and is subject to the terms and conditions in your end user license agreement.
* This source code, and any other information contained herein, shall not be copied, reproduced, published,
* displayed or distributed, in whole or in part, in any medium, by any means, for any purpose except as
* expressly permitted under such license agreement.
*
* Copyright ZIH Corp. 2018
*
* ALL RIGHTS RESERVED
***********************************************/
using Microsoft.Win32;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Device;
using Zebra.Sdk.Printer;
using Zebra.Windows.DevDemo.Utils;
namespace Zebra.Windows.DevDemo.Demos.Profile {
/// <summary>
/// Interaction logic for ProfileView.xaml
/// </summary>
public partial class ProfileView : UserControl {
public ProfileView() {
InitializeComponent();
}
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) { }
}
}
}
private void UploadProfile(string profilePath, Connection connection) {
if (connection != null) {
try {
connection.Open();
ZebraPrinter genericPrinter = ZebraPrinterFactory.GetInstance(connection);
ZebraPrinterLinkOs printer = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter);
if (printer != null) {
printer.LoadProfile(profilePath, FileDeletionOption.NONE, false);
string printerAddress = connectionSelector.getConnectionAddress();
MessageBoxCreator.ShowInformation($"Profile loaded successfully to printer {printerAddress}", "Profile Uploaded Successfully");
} else {
MessageBoxCreator.ShowError("Profile loading 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 (Exception e) {
MessageBoxCreator.ShowError(e.Message, "Upload Profile Error");
} finally {
SetButtonStates(true);
try {
connection.Close();
} catch (ConnectionException) { }
}
}
}
private void SetButtonStates(bool enabled) {
Application.Current.Dispatcher.Invoke(() => {
createProfileButton.IsEnabled = enabled;
uploadProfileButton.IsEnabled = enabled;
});
}
private async void CreateProfileButton_Click(object sender, RoutedEventArgs e) {
try {
SetButtonStates(false);
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == true) {
string profilePath = saveFileDialog.FileName;
Connection connection = connectionSelector.GetConnection();
await Task.Run(() => CreateProfile(profilePath, connection));
} else {
SetButtonStates(true);
}
} catch (Exception ex) {
MessageBoxCreator.ShowError(ex.Message, "Create Profile Error");
SetButtonStates(true);
}
}
private async void UploadProfileButton_Click(object sender, RoutedEventArgs e) {
try {
SetButtonStates(false);
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true) {
string profilePath = openFileDialog.FileName;
Connection connection = connectionSelector.GetConnection();
await Task.Run(() => UploadProfile(profilePath, connection));
} else {
SetButtonStates(true);
}
} catch (Exception ex) {
MessageBoxCreator.ShowError(ex.Message, "Upload Profile Error");
SetButtonStates(true);
}
}
}
}