Skip to content

Commit

Permalink
fix: 自定义串口波特率显示出来, close #132
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxuuu committed May 30, 2023
1 parent 884a3a4 commit acafe33
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions llcom/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
dataShowFrame.Navigate(new Uri("Pages/DataShowPage.xaml", UriKind.Relative));
//加载初始波特率
baudRateComboBox.Text = Tools.Global.setting.baudRate.ToString();
var br = Tools.Global.setting.baudRate.ToString();
if(baudRateComboBox.Items.Contains(br))
baudRateComboBox.Text = Tools.Global.setting.baudRate.ToString();
else
{
lastBaudRateSelectedIndex = baudRateComboBox.Items.Count - 1;//防止弹窗提示
baudRateComboBox.Items[baudRateComboBox.Items.Count - 1] = br;
baudRateComboBox.Text = br;
}
// 绑定事件监听,用于监听HID设备插拔
(PresentationSource.FromVisual(this) as HwndSource)?.AddHook(WndProc);
Expand Down Expand Up @@ -675,40 +683,40 @@ private void ClearLogButton_Click(object sender, RoutedEventArgs e)
Tools.Logger.ClearData();
}

private int lastBaudRateSelectedIndex = -1;
private void BaudRateComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//选的没变
if(lastBaudRateSelectedIndex == baudRateComboBox.SelectedIndex)
return;

if (baudRateComboBox.SelectedItem != null)
{
if ((baudRateComboBox.SelectedItem as ComboBoxItem).Content.ToString() ==
(TryFindResource("OtherRate") as string ?? "?!"))
lastBaudRateSelectedIndex = baudRateComboBox.SelectedIndex;
if (baudRateComboBox.SelectedIndex == baudRateComboBox.Items.Count - 1)
{
int br = 0;
Tuple<bool, string> ret = Tools.InputDialog.OpenDialog(TryFindResource("ShowBaudRate") as string ?? "?!",
"115200", TryFindResource("OtherRate") as string ?? "?!");
if (!ret.Item1 || !int.TryParse(ret.Item2,out br))//啥都没选
{
Tools.MessageBox.Show(TryFindResource("OtherRateFail") as string ?? "?!");
Task.Run(() =>
{
this.Dispatcher.Invoke(new Action(delegate {
baudRateComboBox.Text = Tools.Global.setting.baudRate.ToString();
}));
});
return;
}
Tools.Global.setting.baudRate = br;
if(Tools.Global.setting.baudRate != br)//说明设置失败了
Task.Run(() =>
{
this.Dispatcher.Invoke(new Action(delegate {
baudRateComboBox.Text = Tools.Global.setting.baudRate.ToString();
}));
});
Task.Run(() =>
{
this.Dispatcher.Invoke(new Action(delegate {
var text = Tools.Global.setting.baudRate.ToString();
baudRateComboBox.Items[baudRateComboBox.Items.Count - 1] = text;
baudRateComboBox.Text = text;
}));
});
}
else
{
Tools.Global.setting.baudRate =
int.Parse((baudRateComboBox.SelectedItem as ComboBoxItem).Content.ToString());
baudRateComboBox.Items[baudRateComboBox.Items.Count - 1] = TryFindResource("OtherRate") as string ?? "?!";
}
}
}
Expand Down

0 comments on commit acafe33

Please sign in to comment.