bool SelectItem(string id) { if (id == null) { return(false); } IdInfo info = IdInfo.Parse(id); List <ListViewItem> level1_items = new List <ListViewItem>(); List <ListViewItem> level2_items = new List <ListViewItem>(); // List<ItemAndWeight> results = new List<ItemAndWeight>(); foreach (ListViewItem item in this.listView_tags.Items) { if (info.Prefix == "pii") { // string current_pii = ListViewUtil.GetItemText(item, COLUMN_PII); string current_pii = GetItemPII(item); if (current_pii == info.Text) { level1_items.Add(item); //ListViewUtil.SelectLine(item, true); //return true; } else if (StringUtil.IsInList("auto_or_blankPII", this.AutoSelectCondition) && string.IsNullOrEmpty(current_pii) == true) { level2_items.Add(item); } } if (info.Prefix == "uid") { string current_uid = ListViewUtil.GetItemText(item, COLUMN_UID); if (current_uid == info.Text) { level1_items.Add(item); // ListViewUtil.SelectLine(item, true); // return true; } } } if (level1_items.Count > 0) { ListViewUtil.SelectLine(level1_items[0], true); return(true); } // 只有当 level2 命中精确为一个时才选中。命中多了则无法选择 if (level2_items.Count == 1) { ListViewUtil.SelectLine(level2_items[0], true); return(true); } // 触发提示 if (this.AskTag != null) { AskTagEventArgs e = new AskTagEventArgs(); this.AskTag(this, e); this.ShowMessage(e.Text, "green", true); } return(false); }
// 更新标签列表 bool UpdateChipList(bool show_messageBox) { int nRet = Interlocked.Increment(ref _inUpdate); try { if (nRet != 1) { return(false); } // this.ClearMessage(); string strError = ""; if (string.IsNullOrEmpty(Program.MainForm.RfidCenterUrl)) { strError = "尚未配置 RFID 中心 URL"; goto ERROR1; } RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; goto ERROR1; } try { ListTagsResult result = channel.Object.ListTags("*", null); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } List <Task> tasks = new List <Task>(); bool is_empty = false; bool changed = false; this.Invoke((Action)(() => { is_empty = this.listView_tags.Items.Count == 0; List <ListViewItem> items = new List <ListViewItem>(); foreach (OneTag tag in result.Results) { ListViewItem item = FindItem(this.listView_tags, tag.ReaderName, tag.UID); if (item == null) { item = new ListViewItem(); ListViewUtil.ChangeItemText(item, COLUMN_UID, tag.UID); ListViewUtil.ChangeItemText(item, COLUMN_READERNAME, tag.ReaderName); item.Tag = new ItemInfo { OneTag = tag }; this.listView_tags.Items.Add(item); if (tag.TagInfo == null) { // 启动单独的线程去填充 .TagInfo tasks.Add(Task.Run(() => { GetTagInfo(item); })); } } items.Add(item); changed = true; } // 交叉运算得到比 items 中多出来的 ListViewItem,删除它们 List <ListViewItem> delete_items = new List <ListViewItem>(); foreach (ListViewItem item in this.listView_tags.Items) { if (items.IndexOf(item) == -1) { delete_items.Add(item); } } foreach (ListViewItem item in delete_items) { this.listView_tags.Items.Remove(item); changed = true; } })); // 再建立一个 task,等待 tasks 执行完以后,自动选定一个 item if (tasks.Count > 0 || changed) { Task.Run(() => { Task.WaitAll(tasks.ToArray()); bool closed = false; this.Invoke((Action)(() => { // 首次填充,自动设好选定状态 // if (is_empty) { var ret = SelectItem(this.SelectedID != null ? this.SelectedID : this.SelectedPII); if (// string.IsNullOrEmpty(this.SelectedPII) == false ret == true && this.AutoCloseDialog) { this.button_OK_Click(this, new EventArgs()); closed = true; } } })); if (closed == false) { //this.Invoke((Action)(() => //{ FillEntityInfo(); //})); if (this._mode == "auto_fix_eas") { this.Invoke((Action)(() => { AutoFixEas(); })); } } }); } else { if (this.AskTag != null) { AskTagEventArgs e = new AskTagEventArgs(); this.AskTag(this, e); this.ShowMessage(e.Text, "green", true); } } return(true); } catch (RemotingException ex) { strError = "UpdateChipList() 出现异常: " + ex.Message; goto ERROR1; } catch (Exception ex) { strError = "UpdateChipList() 出现异常: " + ExceptionUtil.GetDebugText(ex); goto ERROR1; } finally { EndRfidChannel(channel); } ERROR1: if (show_messageBox) { this.ShowMessageBox(strError); } else { this.ShowMessage(strError, "red", true); } return(false); } finally { Interlocked.Decrement(ref _inUpdate); } }