async void menu_refreshServerName(object sender, System.EventArgs e) { string strError = ""; if (listView1.SelectedIndices.Count == 0) { strError = "尚未选择要刷新服务器名的事项 ..."; goto ERROR1; } int change_count = 0; List <string> errors = new List <string>(); using (MessageBar bar = MessageBar.Create(this, "正在刷新服务器名")) { foreach (int index in this.listView1.SelectedIndices) { dp2Server server = Servers[index] as dp2Server; // 获得服务器 UID string server_name = ""; bar.SetMessageText($"正在获取服务器 {server.Url} 的图书馆名 ..."); var result = await ServerDlg.GetServerName(server.Url); if (result.Value == -1) { errors.Add($"针对服务器 {server.Url} 获取图书馆名时出错: {result.ErrorInfo}"); continue; } else { server_name = result.ErrorCode; } if (server.Name != server_name) { server.Name = server_name; change_count++; } } // TODO: 刷新后如果发现发生了服务器名重复,怎么处理? if (change_count > 0) { Servers.Changed = true; // 刷新前保存选择位置 var indices = ListViewUtil.GetSelectedIndices(this.listView1); this.listView1.BeginUpdate(); FillList(); // 刷新后恢复选择位置 ListViewUtil.SelectItems(this.listView1, indices); this.listView1.EndUpdate(); } } if (errors.Count > 0) { strError = $"刷新服务器名时出错:\r\n{StringUtil.MakePathList(errors, "\r\n")}"; goto ERROR1; } return; ERROR1: // TODO: MessageDlg 应可以指定左上角图标 MessageDlg.Show(this, strError, "ServersDlg"); }