|
@@ -0,0 +1,177 @@
|
|
|
+using System.Windows;
|
|
|
+using System.Windows.Forms;
|
|
|
+using System.Windows.Media;
|
|
|
+
|
|
|
+namespace Theatre
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// SettingsWindow.xaml 的交互逻辑
|
|
|
+ /// </summary>
|
|
|
+ public partial class SettingsWindow : Window
|
|
|
+ {
|
|
|
+ private readonly AppContext _appContext = AppContext.GetAppContext();
|
|
|
+ private readonly SerialTool _serialTool = SerialTool.GetSerialTool();
|
|
|
+
|
|
|
+ private OpenFileDialog _ofd;
|
|
|
+
|
|
|
+ public SettingsWindow()
|
|
|
+ {
|
|
|
+ InitializeComponent();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ _ofd = new OpenFileDialog
|
|
|
+ {
|
|
|
+ Filter = Properties.Resources.SettingsWindow_Audio_Filter,
|
|
|
+ ValidateNames = true,
|
|
|
+ CheckPathExists = true,
|
|
|
+ CheckFileExists = true,
|
|
|
+ Multiselect = false
|
|
|
+ };
|
|
|
+
|
|
|
+ Audio1.Text = Properties.Settings.Default.Audio1;
|
|
|
+ Audio2.Text = Properties.Settings.Default.Audio2;
|
|
|
+ Audio3.Text = Properties.Settings.Default.Audio3;
|
|
|
+ Audio4.Text = Properties.Settings.Default.Audio4;
|
|
|
+ Audio5.Text = Properties.Settings.Default.Audio5;
|
|
|
+
|
|
|
+ if (_appContext.PortOpen)
|
|
|
+ {
|
|
|
+ Devices.Items.Clear();
|
|
|
+ Devices.Items.Add(_appContext.SerialPort.PortName);
|
|
|
+ Devices.SelectedIndex = 0;
|
|
|
+ Devices.IsEnabled = false;
|
|
|
+ Connect.Content = "断开连接";
|
|
|
+ Connect.Foreground = Brushes.Red;
|
|
|
+ Scan.IsEnabled = false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Connect.Content = "连接设备";
|
|
|
+ Connect.Foreground = Brushes.Green;
|
|
|
+ Scan_Click(null, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
|
+ {
|
|
|
+ Properties.Settings.Default.Save();
|
|
|
+ Properties.Settings.Default.Reload();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Scan_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ // 清除当前串口号中的所有串口名称
|
|
|
+ Devices.Items.Clear();
|
|
|
+ var portList = _serialTool.FindPort();
|
|
|
+ if (portList.Count == 0)
|
|
|
+ {
|
|
|
+ System.Windows.Forms.MessageBox.Show("没有找到可用设备!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ portList.ForEach(
|
|
|
+ delegate (string portName) {
|
|
|
+ Devices.Items.Add(portName);
|
|
|
+ });
|
|
|
+ //使 ListBox 显示第1个添加的索引
|
|
|
+ Devices.SelectedIndex = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Connect_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (_appContext.PortOpen)
|
|
|
+ {
|
|
|
+ // 关闭串口
|
|
|
+ if (_serialTool.ClosePort(_appContext.SerialPort))
|
|
|
+ {
|
|
|
+ Devices.IsEnabled = true;
|
|
|
+ Scan.IsEnabled = true;
|
|
|
+ Scan_Click(null, null);
|
|
|
+ Connect.Content = "连接设备";
|
|
|
+ Connect.Foreground = Brushes.Green;
|
|
|
+ _appContext.SerialPort = null;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ System.Windows.Forms.MessageBox.Show("断开连接失败!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 检测串口设置
|
|
|
+ var portName = Devices.Text.Trim();
|
|
|
+ if (portName == "")
|
|
|
+ {
|
|
|
+ System.Windows.Forms.MessageBox.Show("没有发现可连接设备!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 打开串口
|
|
|
+ var serialPort = _serialTool.OpenPort(portName, "9600", "8", "1", "无", _appContext.DataReceived);
|
|
|
+ if (serialPort == null)
|
|
|
+ {
|
|
|
+ // 打开串口失败
|
|
|
+ System.Windows.Forms.MessageBox.Show("连接设备失败!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Devices.IsEnabled = false;
|
|
|
+ Scan.IsEnabled = false;
|
|
|
+ Connect.Content = "断开连接";
|
|
|
+ Connect.Foreground = Brushes.Red; ;
|
|
|
+ _appContext.SerialPort = serialPort;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Audio1Choose_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (_ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ var strFileName = _ofd.FileName;
|
|
|
+ Audio1.Text = strFileName;
|
|
|
+ Properties.Settings.Default.Audio1 = strFileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Audio2Choose_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (_ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ var strFileName = _ofd.FileName;
|
|
|
+ Audio2.Text = strFileName;
|
|
|
+ Properties.Settings.Default.Audio2 = strFileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Audio3Choose_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (_ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ var strFileName = _ofd.FileName;
|
|
|
+ Audio3.Text = strFileName;
|
|
|
+ Properties.Settings.Default.Audio3 = strFileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Audio4Choose_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (_ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ var strFileName = _ofd.FileName;
|
|
|
+ Audio4.Text = strFileName;
|
|
|
+ Properties.Settings.Default.Audio4 = strFileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Audio5Choose_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (_ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ var strFileName = _ofd.FileName;
|
|
|
+ Audio5.Text = strFileName;
|
|
|
+ Properties.Settings.Default.Audio5 = strFileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Audio6Choose_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (_ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ var strFileName = _ofd.FileName;
|
|
|
+ Audio6.Text = strFileName;
|
|
|
+ Properties.Settings.Default.Audio6 = strFileName;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|