alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
 
using AddInPlugin.Util;
using AddInPlugin;
using Microsoft.Win32;
using OpenTap.Addin.Listener;
using OpenTap.Addin.Reader;
using OpenTap.Addin.Util;
using OpenTap.Addin;
using OpenTap;
using System.Collections.ObjectModel;
using System.Threading;
using System;
using System.Windows;
 
namespace TestSeq
{
    public class Net47Getter : IRegGetter
    {
        public string GetAppPath(string keyPath)
        {
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath))
            {
                if (key == null)
                {
                    throw new Exception("注册表不存在,请先运行一次编辑器");
                }
 
                string value = key.GetValue("Location")?.ToString();
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("注册表不存在,请先运行一次编辑器");
                }
                return value;
            }
        }
    }
 
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public const int MaxLogCount = 1000;
 
        private readonly ObservableCollection<string> _logItems = new ObservableCollection<string>();
 
        public MainWindow()
        {
            RegUtil.Register(new Net47Getter());
            StationGlobalsManager.Register(new DefaultStationGlobalsManager());
            VariableResolverFactory.Register(new DefaultVariableResolverFactory());
            InitializeComponent();
            LogListView.ItemsSource = _logItems;
        }
 
        private void self_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            tapThread?.Abort();
            tapThread = null;
            AppendLog("已停止");
        }
 
        /// <summary>
        /// 打开按钮:选择 *.TestPlan 文件,并将路径显示在 PathTextBlock 中。
        /// </summary>
        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog
            {
                Title = "选择 TapPlan 文件",
                Filter = "TapPlan 文件 (*.TapPlan)|*.TapPlan|所有文件 (*.*)|*.*",
                CheckFileExists = true,
                Multiselect = false
            };
 
            if (dlg.ShowDialog(this) == true)
            {
                PathTextBlock.Text = dlg.FileName;
            }
        }
 
        private TapThread tapThread;
 
        // TODO: 执行按钮逻辑,由你自己编写
        private void RunButton_Click(object sender, RoutedEventArgs e)
        {
            _logItems.Clear();
            var uiMessageHandler = PostUIMessageHandler.Get();
            uiMessageHandler.OnPostUIMsg += UiMessageHandler_OnPostUIMsg;
            TestPlan plan = TestPlanReader.ReadTestPlan(PathTextBlock.Text);
 
            tapThread = TapThread.Start(() =>
            {
                ResultSettings.Current.Add(uiMessageHandler);
                var run = plan.Execute(ResultSettings.Current, null, null, null);
            });
            AppendLog($"执行{PathTextBlock.Text}...");
        }
 
        // TODO: 停止按钮逻辑,由你自己编写
        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            tapThread?.Abort();
            tapThread = null;
            AppendLog("已停止");
        }
 
        /// <summary>
        /// 追加一行日志到日志区域,可在任意线程调用。
        /// </summary>
        public void AppendLog(string message)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.Invoke(() => AppendLog(message));
                return;
            }
 
            string line = $"[{DateTime.Now:HH:mm:ss}] {message}";
            _logItems.Add(line);
            while (_logItems.Count > MaxLogCount)
            {
                _logItems.RemoveAt(0);
            }
            LogListView.ScrollIntoView(_logItems[_logItems.Count - 1]);
        }
 
        private void UiMessageHandler_OnPostUIMsg(object sender, PostUIMsg e)
        {
            AppendLog($"string:{e.StringData} bool:{e.BoolData} number:{e.NumberData}");
        }
    }
}