|
| 1 | +# HOST事件 |
| 2 | +## 一、禁止/生效 |
| 3 | +1、在Main类中增加公有静态方法changeRuleEnableByIndex |
| 4 | +``` |
| 5 | +//变更Rule的Enable数据 |
| 6 | +public void changeRuleEnableByIndex(int index) |
| 7 | +{ |
| 8 | + //获取数据 |
| 9 | + HostModel rule = mainData[index] as HostModel; |
| 10 | + //变更状态 |
| 11 | + rule.Enable = !rule.Enable; |
| 12 | + //重新写入文件 |
| 13 | + DataTool.writeConfigToFile(); |
| 14 | +} |
| 15 | +``` |
| 16 | +2、新建GlobalEvent类,添加partial前缀,并且增加私有方法changeRuleEnable |
| 17 | +``` |
| 18 | +//是否生效事件 |
| 19 | +private void changeRuleEnable(object sender, MouseButtonEventArgs e) |
| 20 | +{ |
| 21 | + int index = Convert.ToInt32((sender as Rectangle).Tag.ToString()); |
| 22 | + //变更状态 |
| 23 | + Main.changeRuleEnableByIndex(index); |
| 24 | +} |
| 25 | +``` |
| 26 | +3、修改GlobalStyle.xaml中事件绑定类为GlobalEvent |
| 27 | +4、给选中框添加MouseLeftButtonDown事件 |
| 28 | +``` |
| 29 | +<Rectangle Grid.Column="2" Tag="{Binding Path=Index}" MouseLeftButtonDown="changeRuleEnable" Visibility="{Binding Path=CheckHide}" Style="{StaticResource content_style_rect}"></Rectangle> |
| 30 | +<Canvas Grid.Column="2" Width="12" Height="12" Visibility="{Binding Path=CheckShow}"> |
| 31 | + <Path Fill="#FFEC8E72" Data="M10.125 1.5l-5.625 5.625-2.625-2.625-1.875 1.875 4.5 4.5 7.5-7.5z" /> |
| 32 | +</Canvas> |
| 33 | +<Rectangle Grid.Column="2" Tag="{Binding Path=Index}" MouseLeftButtonDown="changeRuleEnable" Visibility="{Binding Path=CheckShow}" Style="{StaticResource content_style_rect_check}"></Rectangle> |
| 34 | +``` |
| 35 | +5、打包预览 |
| 36 | + |
| 37 | +## 二、双击修改 |
| 38 | +1、HostAlertUI类的构造函数增加index参数,并设置默认值为-1 |
| 39 | +2、HostAlertUI类添加_index属性,并在构造函数中初始化 |
| 40 | +``` |
| 41 | +private int _index; |
| 42 | +public HostAlertUI(int index = -1) |
| 43 | +{ |
| 44 | + _index = index; |
| 45 | +
|
| 46 | + InitializeComponent(); |
| 47 | +} |
| 48 | +``` |
| 49 | +3、HostAlertUI类增加initInputText方法,并在构造函数中调用 |
| 50 | +``` |
| 51 | +#region 初始化输入框内容 |
| 52 | +private void initInputText() |
| 53 | +{ |
| 54 | + //小于0代表是新增,直接返回 |
| 55 | + if (_index < 0) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | +
|
| 60 | + //获取数据 |
| 61 | + HostModel rule = Main.mainData[_index] as HostModel; |
| 62 | +
|
| 63 | + //设置数据 |
| 64 | + this.ip.Text = rule.IP; |
| 65 | + this.port.Text = rule.Port; |
| 66 | + this.url.Text = rule.Url; |
| 67 | +} |
| 68 | +#endregion |
| 69 | +``` |
| 70 | +4、Main类中增加公有静态方法modifyRuleByIndex |
| 71 | +``` |
| 72 | +//修改Rule数据 |
| 73 | +public static void modifyRuleByIndex(int index, string ip, string port, string url) |
| 74 | +{ |
| 75 | + //获取规则 |
| 76 | + HostModel rule = mainData[index] as HostModel; |
| 77 | + //更新数据 |
| 78 | + rule.IP = ip; |
| 79 | + rule.Port = port; |
| 80 | + rule.Url = url; |
| 81 | + //重新写入文件 |
| 82 | + DataTool.writeConfigToFile(); |
| 83 | +} |
| 84 | +``` |
| 85 | +5、修改HostAlertUI类的addHostRule方法,增加修改相关逻辑 |
| 86 | +6、AlertTool类的showHostAlertUI方法增加index参数,设置默认值为-1,并传递给HostAlertUI |
| 87 | +7、GlobalEvent类增加私有方法modifyRule |
| 88 | +``` |
| 89 | +//修改规则 |
| 90 | +private void modifyRule(object sender, MouseButtonEventArgs e) |
| 91 | +{ |
| 92 | + int index = Convert.ToInt32((sender as Label).Tag.ToString()); |
| 93 | + //显示弹框 |
| 94 | + AlertTool.showHostAlertUI(index); |
| 95 | +} |
| 96 | +``` |
| 97 | +8、在GlobalStyle中给对应控件绑定双击事件 |
| 98 | +``` |
| 99 | +<Label Grid.Column="0" Tag="{Binding Path=Index}" MouseDoubleClick="modifyRule" Content="{Binding Path=IpAndPort}" Template="{StaticResource content_text}"></Label> |
| 100 | +<Label Grid.Column="1" Tag="{Binding Path=Index}" MouseDoubleClick="modifyRule" Content="{Binding Path=TipsAndUrl}" Template="{StaticResource content_text}"></Label> |
| 101 | +``` |
| 102 | +9、打包预览 |
| 103 | + |
| 104 | + |
| 105 | +## 三、菜单 |
| 106 | +1、项目右键选择属性,切换到资源Tab,点击创建资源文件,创建完成以后,切换为图像资源,然后点击添加资源,选择添加现有文件,导入modify、delete、up、down四个Icon |
| 107 | + |
| 108 | +2、选择四个新增的Icon图片,修改复制到输出目录属性为始终复制,修改生成操作属性为Resource |
| 109 | + |
| 110 | +3、在GlobalStyle中新增menu_rule定义 |
| 111 | +``` |
| 112 | +<!-- Rule菜单 --> |
| 113 | +<ContextMenu x:Key="menu_rule"> |
| 114 | + <MenuItem Header="修改"> |
| 115 | + <MenuItem.Icon> |
| 116 | + <Image Source="Resources/modify.png" Width="16" Height="16"></Image> |
| 117 | + </MenuItem.Icon> |
| 118 | + </MenuItem> |
| 119 | + <MenuItem Header="删除"> |
| 120 | + <MenuItem.Icon> |
| 121 | + <Image Source="Resources/delete.png" Width="16" Height="16"></Image> |
| 122 | + </MenuItem.Icon> |
| 123 | + </MenuItem> |
| 124 | + <Separator></Separator> |
| 125 | + <MenuItem Header="上移"> |
| 126 | + <MenuItem.Icon> |
| 127 | + <Image Source="Resources/up.png" Width="16" Height="16"></Image> |
| 128 | + </MenuItem.Icon> |
| 129 | + </MenuItem> |
| 130 | + <MenuItem Header="下移"> |
| 131 | + <MenuItem.Icon> |
| 132 | + <Image Source="Resources/down.png" Width="16" Height="16"></Image> |
| 133 | + </MenuItem.Icon> |
| 134 | + </MenuItem> |
| 135 | +</ContextMenu> |
| 136 | +``` |
| 137 | +4、在GlobalStyle中给对应控件绑定ContextMenu属性 |
| 138 | +``` |
| 139 | +<Label Grid.Column="0" Tag="{Binding Path=Index}" MouseDoubleClick="modifyRule" Content="{Binding Path=IpAndPort}" ContextMenu="{StaticResource menu_rule}" Template="{StaticResource content_text}"></Label> |
| 140 | +<Label Grid.Column="1" Tag="{Binding Path=Index}" MouseDoubleClick="modifyRule" Content="{Binding Path=TipsAndUrl}" ContextMenu="{StaticResource menu_rule}" Template="{StaticResource content_text}"></Label> |
| 141 | +``` |
| 142 | +5、打包预览 |
| 143 | + |
| 144 | +## 四、菜单--修改 |
| 145 | +1、GlobalEvent类增加私有方法handleRuleMenuClick |
| 146 | +``` |
| 147 | +#region 菜单点击事件 |
| 148 | +private void handleRuleMenuClick(object sender, RoutedEventArgs e) |
| 149 | +{ |
| 150 | + string type = (sender as MenuItem).Tag.ToString(); |
| 151 | + object target = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as object;//获取点击源控件 |
| 152 | + int index = (int)(target as Label).Tag; |
| 153 | +
|
| 154 | + if (type == "modify") |
| 155 | + { |
| 156 | + modifyRule(target, null); |
| 157 | + } |
| 158 | +} |
| 159 | +#endregion |
| 160 | +``` |
| 161 | +2、修改菜单Item绑定Click,并设置Tag为modify |
| 162 | +``` |
| 163 | +<MenuItem Header="修改" Click="handleRuleMenuClick" Tag="modify"> |
| 164 | + <MenuItem.Icon> |
| 165 | + <Image Source="Resources/modify.png" Width="16" Height="16"></Image> |
| 166 | + </MenuItem.Icon> |
| 167 | +</MenuItem> |
| 168 | +``` |
| 169 | +## 五、菜单--删除 |
| 170 | +1、Main类中添加公有静态方法deleteRuleByIndex |
| 171 | +``` |
| 172 | +//删除Rule |
| 173 | +public static void deleteRuleByIndex(int index) |
| 174 | +{ |
| 175 | + //删除对应的数据 |
| 176 | + mainData.RemoveAt(index); |
| 177 | +
|
| 178 | + //遍历修改下标值 |
| 179 | + for (int i = 0, len = mainData.Count; i < len; i++) |
| 180 | + { |
| 181 | + HostModel item = mainData[i] as HostModel; |
| 182 | + item.Index = i; |
| 183 | + } |
| 184 | +
|
| 185 | + //重新写入文件 |
| 186 | + DataTool.writeConfigToFile(); |
| 187 | +} |
| 188 | +``` |
| 189 | +2、Container类中添加公有方法deleteRuleFromUI |
| 190 | +``` |
| 191 | +//删除Rule控件 |
| 192 | +public void deleteRuleFromUI(int index) |
| 193 | +{ |
| 194 | + this.host.Children.RemoveAt(index); |
| 195 | +} |
| 196 | +``` |
| 197 | +3、GlobalEvent类的handleRuleMenuClick增加delete逻辑 |
| 198 | +``` |
| 199 | +else if (type == "delete") |
| 200 | +{ |
| 201 | + //删除数据 |
| 202 | + Main.deleteRuleByIndex(index); |
| 203 | + //删除对应UI |
| 204 | + Main.container.deleteRuleFromUI(index); |
| 205 | +} |
| 206 | +``` |
| 207 | +4、GlobalStyle中的删除菜单Item绑定Click属性,并设置Tag为delete |
| 208 | +``` |
| 209 | +<MenuItem Header="删除" Click="handleRuleMenuClick" Tag="delete"> |
| 210 | + <MenuItem.Icon> |
| 211 | + <Image Source="Resources/delete.png" Width="16" Height="16"></Image> |
| 212 | + </MenuItem.Icon> |
| 213 | +</MenuItem> |
| 214 | +``` |
| 215 | +## 六、菜单--上移/下移 |
| 216 | +1、Main类中添加公有静态方法moveRuleByType |
| 217 | +``` |
| 218 | +//移动Rule |
| 219 | +public static void moveRuleByType(int index, string moveType) |
| 220 | +{ |
| 221 | + //第一个数据 |
| 222 | + if (index == 0 && moveType == "up") |
| 223 | + { |
| 224 | + return; |
| 225 | + } |
| 226 | +
|
| 227 | + //最后一个数据 |
| 228 | + if (index == mainData.Count - 1 && moveType == "down") |
| 229 | + { |
| 230 | + return; |
| 231 | + } |
| 232 | +
|
| 233 | + //移动数据 |
| 234 | + if (moveType == "up") |
| 235 | + { |
| 236 | + mainData.Insert(index - 1, mainData[index]); |
| 237 | + mainData.RemoveAt(index + 1); |
| 238 | + } |
| 239 | + else |
| 240 | + { |
| 241 | + mainData.Insert(index, mainData[index + 1]); |
| 242 | + mainData.RemoveAt(index + 2); |
| 243 | + } |
| 244 | +
|
| 245 | + //遍历修改下标值 |
| 246 | + for (int i = 0, len = mainData.Count; i < len; i++) |
| 247 | + { |
| 248 | + HostModel item = mainData[i] as HostModel; |
| 249 | + item.Index = i; |
| 250 | + } |
| 251 | +
|
| 252 | + //重新写入文件 |
| 253 | + DataTool.writeConfigToFile(); |
| 254 | +} |
| 255 | +``` |
| 256 | +2、Container类中添加私有方法initRuleToUI |
| 257 | +``` |
| 258 | +#region 私有方法(内部工具方法) |
| 259 | +//初始化Rule面板 |
| 260 | +private void initRuleToUI() |
| 261 | +{ |
| 262 | + ArrayList items = Main.mainData; |
| 263 | +
|
| 264 | + //遍历添加Rule到UI |
| 265 | + for (int i = 0, len = items.Count; i < len; i++) |
| 266 | + { |
| 267 | + addHostRule(items[i] as HostModel); |
| 268 | + } |
| 269 | +} |
| 270 | +#endregion |
| 271 | +``` |
| 272 | +3、Container类中添加公有方法moveRuleFromUI |
| 273 | +``` |
| 274 | +//移动Rule控件 |
| 275 | +public void moveRuleFromUI(int index, string moveType) |
| 276 | +{ |
| 277 | + if (index <= 0 && moveType == "up") |
| 278 | + { |
| 279 | + Fiddler.FiddlerApplication.DoNotifyUser("已在最顶部", "无法上移"); |
| 280 | + return; |
| 281 | + } |
| 282 | +
|
| 283 | + StackPanel panel = this.host; |
| 284 | +
|
| 285 | + if (index == panel.Children.Count - 1 && moveType == "down") |
| 286 | + { |
| 287 | + Fiddler.FiddlerApplication.DoNotifyUser("已在最底部", "无法下移"); |
| 288 | + return; |
| 289 | + } |
| 290 | +
|
| 291 | + //移除所有的Item |
| 292 | + panel.Children.Clear(); |
| 293 | + //重新渲染所有的Item |
| 294 | + initRuleToUI(); |
| 295 | +} |
| 296 | +``` |
| 297 | +4、GlobalEvent类的handleRuleMenuClick增加up/down逻辑 |
| 298 | +``` |
| 299 | +else if (type == "up" || type == "down") |
| 300 | +{ |
| 301 | + //移动对应的数据 |
| 302 | + Main.moveRuleByType(index, type); |
| 303 | + //移动对应的UI |
| 304 | + Main.container.moveRuleFromUI(index, type); |
| 305 | +} |
| 306 | +``` |
| 307 | +5、GlobalStyle中的上移/下移菜单Item绑定Click属性,并设置Tag为up/down |
| 308 | +``` |
| 309 | +<MenuItem Header="上移" Click="handleRuleMenuClick" Tag="up"> |
| 310 | + <MenuItem.Icon> |
| 311 | + <Image Source="Resources/up.png" Width="16" Height="16"></Image> |
| 312 | + </MenuItem.Icon> |
| 313 | +</MenuItem> |
| 314 | +<MenuItem Header="下移" Click="handleRuleMenuClick" Tag="down"> |
| 315 | + <MenuItem.Icon> |
| 316 | + <Image Source="Resources/down.png" Width="16" Height="16"></Image> |
| 317 | + </MenuItem.Icon> |
| 318 | +</MenuItem> |
| 319 | +``` |
0 commit comments