SettingsView.xaml.cs
7.1 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***********************************************
* 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 System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;
using Zebra.Sdk.Settings;
using Zebra.Windows.DevDemo.Utils;
namespace Zebra.Windows.DevDemo.Demos.Settings {
/// <summary>
/// Interaction logic for SettingsView.xaml
/// </summary>
public partial class SettingsView : UserControl {
private SettingsViewModel viewModel;
public SettingsView() {
InitializeComponent();
viewModel = DataContext as SettingsViewModel;
}
private void UpdateSettingsTable() {
Connection connection = null;
try {
connection = connectionSelector.GetConnection();
connection.Open();
ZebraPrinter genericPrinter = ZebraPrinterFactory.GetInstance(connection);
ZebraPrinterLinkOs printer = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter);
if (printer != null) {
Dictionary<string, Sdk.Settings.Setting> settings = printer.GetAllSettings();
Application.Current.Dispatcher.Invoke(() => {
if (settings != null) {
foreach (string key in settings.Keys) {
viewModel.Settings.Add(new Setting { Key = key, Value = settings[key].Value, Range = printer.GetSettingRange(key) });
}
} else {
MessageBoxCreator.ShowError("Error reading settings", "Settings Error");
}
printerSettingsGrid.UnselectAll();
});
} else {
MessageBoxCreator.ShowError("Connected printer does not support settings", "Connection Error");
}
} catch (ConnectionException e) {
MessageBoxCreator.ShowError(e.Message, "Connection Error");
} catch (ZebraPrinterLanguageUnknownException e) {
MessageBoxCreator.ShowError(e.Message, "Connection Error");
} catch (SettingsException e) {
MessageBoxCreator.ShowError(e.Message, "Settings Error");
} catch (Exception e) {
MessageBoxCreator.ShowError(e.Message, "Save Settings Error");
} finally {
SetButtonStates(true);
try {
if (connection != null) {
connection.Close();
}
} catch (ConnectionException) { }
}
}
private void ClearSettings() {
Application.Current.Dispatcher.Invoke(() => {
viewModel.Settings.Clear();
});
}
private bool SaveModifiedSettingsToPrinter() {
bool result = false;
Connection connection = null;
try {
connection = connectionSelector.GetConnection();
connection.Open();
ZebraPrinter genericPrinter = ZebraPrinterFactory.GetInstance(connection);
ZebraPrinterLinkOs printer = ZebraPrinterFactory.CreateLinkOsPrinter(genericPrinter);
if (printer != null) {
foreach (string key in viewModel.ModifiedSettings.Keys) {
if (printer.IsSettingReadOnly(key) == false) {
printer.SetSetting(key, viewModel.ModifiedSettings[key]);
}
}
viewModel.ModifiedSettings.Clear();
result = true;
} else {
MessageBoxCreator.ShowError("Connected printer does not support settings", "Connection Error");
}
} catch (ConnectionException e) {
MessageBoxCreator.ShowError(e.Message, "Connection Error");
} catch (ZebraPrinterLanguageUnknownException e) {
MessageBoxCreator.ShowError(e.Message, "Connection Error");
} catch (SettingsException e) {
MessageBoxCreator.ShowError(e.Message, "Settings Error");
} catch (Exception e) {
MessageBoxCreator.ShowError(e.Message, "Save Settings Error");
} finally {
try {
if (connection != null) {
connection.Close();
}
} catch (ConnectionException) { }
}
return result;
}
private void SetButtonStates(bool enabled) {
Application.Current.Dispatcher.Invoke(() => {
saveSettingsButton.IsEnabled = enabled;
getSettingsButton.IsEnabled = enabled;
});
}
private async void SaveSettingsButton_Click(object sender, RoutedEventArgs e) {
try {
SetButtonStates(false);
if (await Task.Run(() => SaveModifiedSettingsToPrinter())) {
ClearSettings();
await Task.Run(() => UpdateSettingsTable());
} else {
SetButtonStates(true);
}
} catch (Exception ex) {
MessageBoxCreator.ShowError(ex.Message, "Save Settings Error");
SetButtonStates(true);
}
}
private async void GetSettingsButton_Click(object sender, RoutedEventArgs e) {
try {
SetButtonStates(false);
ClearSettings();
await Task.Run(() => UpdateSettingsTable());
} catch (Exception ex) {
MessageBoxCreator.ShowError(ex.Message, "Get Settings Error");
SetButtonStates(true);
}
}
private void PrinterSettingsGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
if (viewModel.Settings.Count > 0) {
TextBox textBox = e.EditingElement as TextBox;
string newValue = textBox.Text.ToString();
Setting setting = e.Row.Item as Setting;
string key = setting.Key;
if (key != null) {
if (viewModel.ModifiedSettings.ContainsKey(key)) {
viewModel.ModifiedSettings[key] = newValue;
} else {
viewModel.ModifiedSettings.Add(key, newValue);
}
}
}
}
}
}