DlgSearchDevices.xaml.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace MV485.Dlg
  15. {
  16. /// <summary>
  17. /// DlgSearchDevices.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class DlgSearchDevices : Window
  20. {
  21. public event Action OnStopTask; // 用于通知任务被停止
  22. public DlgSearchDevices()
  23. {
  24. InitializeComponent();
  25. }
  26. public void UpdateProgress(int currentNum, int totalNum)
  27. {
  28. Dispatcher.BeginInvoke(new Action(() =>
  29. {
  30. double progress = (double)currentNum / totalNum * 100;
  31. progressBar.Value = progress;
  32. progressText.Text = $"正在搜素:{currentNum}/{totalNum}";
  33. }));
  34. }
  35. private void StopButton_Click(object sender, RoutedEventArgs e)
  36. {
  37. stopButton.IsEnabled = false; //防止连续点击
  38. // 触发停止任务事件
  39. OnStopTask?.Invoke();
  40. // 可选:更新状态,提示任务已停止
  41. progressText.Text = "任务已停止";
  42. }
  43. protected override void OnClosed(EventArgs e)
  44. {
  45. base.OnClosed(e);
  46. // 恢复主界面操作
  47. Application.Current.MainWindow.IsEnabled = true;
  48. }
  49. //--------------------------------------------------------
  50. }
  51. }