有没有办法提高我的简单文本过滤器的性能?
我正在编写一个过滤器来挑选项目。我有一个对象列表。这些对象包含数字、名称和一些其他不相关的项目。目前,该列表包含 200 项。在文本框
中输入时,我正在查看字符串是否与列表中对象的数字/名称的一部分匹配。如果是这样,请将它们添加到列表框
。下面是我的文本框 textchanged 事件的代码:
private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
{
lstOverview.Items.Clear();
string data = "";
foreach (ucTelListItem telList in _allUsers)
{
data = telList.User.H323 + telList.user.E164;
if (data.Contains(txtTelnumber.Text))
lstOverview.Items.Add(telList);
}
}
有时在输入字符时会出现一点延迟,特别是当我从 4 条记录变为 200 条记录时(因此,当我有一个过滤器且 4 条记录匹配时,我退格并显示整个列表)再次)。 我的列表是用户控件列表,因为我发现从列表加载用户控件所需的时间更少,然后每次都必须初始化一个新的用户控件。
我可以对代码做一些处理,还是只是将 usercontrol
添加到 listbox
导致小延迟(小延迟 = <1 秒)?
提前致谢。
编辑 我已经编辑了帖子,它是wpf。将项目放入列表并设置项目源并不能解决问题。
I'm writing a filter that will pick out items. I have a list of Objects. The objects contain a number, name and some other irrelevant items. At the moment, the list contains 200 items. When typing in a textbox
, i'm looking if the string matches a part of the number/name of the objects in the list. If so, add them to the listbox
. Here's the code for my textbox textchanged event :
private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
{
lstOverview.Items.Clear();
string data = "";
foreach (ucTelListItem telList in _allUsers)
{
data = telList.User.H323 + telList.user.E164;
if (data.Contains(txtTelnumber.Text))
lstOverview.Items.Add(telList);
}
}
I sometimes see a little delay when entering a character, especially when i go from 4 records to 200 records (so when i had a filter and 4 records matched, and i backspace and the whole list appears again).
My list is a list of usercontrols, cause I found it takes less time to load the usercontrols from a list, then to have to initialize a new usercontrol each time.
Can I do something about the code, or is it just adding the usercontrol
the listbox
that causes the small delay (small delay = <1 sec)?
Thanks in advance.
Edit
I've edited post, it's wpf. And putting items in a list and setting the itemssource isn't solving the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议您同时使用两种技术:
BeginUpdate()
方法,并调用其EndUpdate( )
当您完成添加项目时。这些方法专门用于避免大量插入项目期间的性能损失。TextBox
的最后一个KeyUp
事件经过特定时间后才开始。通过这种方式,您可以增加不评估对用户尚不重要的过滤器的机会。I'd suggest you two techniques to be used in concert:
ListBox
invoke theBeginUpdate()
method, and invoke itsEndUpdate()
when you finish adding items. These methods are specifically there for avoiding performance loss during a massive insertion of items.KeyUp
event of theTextBox
. In this way you augment the chance of not evaluating a filter that is not yet significant to the user.我刚刚找出导致将项目加载到列表框中的延迟的原因。我正在使用预定义的主题(Wpf 主题),并且因为我的列表框都是拉皮条的,所以重新绘制需要更长的时间。所以这与编程逻辑无关,只是样式延迟了我的过滤器。
I have just figured out what caused the delay on loading items into my listbox. I'm using predefined themes (Wpf Themes) and because my listbox is all pimped, it takes longer to be redrawn. So it's nothing to do with programming logic, just the style is delaying my filter.
使用 BeingUpdate/EndUpdate 包装代码,以在添加项目时停止重绘。
Wrap your code with BeingUpdate/EndUpdate to stop redrawing when adding the items.
尝试上面的代码
try the above code
你只有200件???在 WPF 中,您不应该遇到任何性能滞后。只需将数据填充到 ObservableCollection 中,然后将其绑定到列表视图即可。现在,在您的 textchanged 事件中,您可以应用相同的过滤器逻辑,但应用到 ObservableCollection 而不是列表视图。列表视图应立即反映更改。
我正在处理数百万条记录,没有任何延迟。
您永远不想造成延迟
另请参阅VirtualMode 属性用于高级操作。
更新
看来您正在每个 textchanged 事件中执行此操作
data = telList.User.H323 + telList.user.E164;
。您可以更好地预先创建一个List
并仅在循环内实现您的过滤器逻辑。You have only 200 items??? You should not be experiencing any lag in performance then in WPF. Just populate your data into an ObservableCollection and inturn bind it to the listview. Now in your textchanged event you can apply the same filter logic but to the ObservableCollection instead of the listview. The listview should reflect the changes instantly.
I'm working with millions of records without any lag.
You never want to create delays
Also take a look at VirtualMode property for advanced operations.
Update
And it seems you're doing this operation
data = telList.User.H323 + telList.user.E164;
in every textchanged event. You can better create aList<data>
beforehand and implement only your filter logic inside the loop.您必须首先使用 lambda 从列表中获取所有相关项目,然后尝试使用 AddRange 将项目添加到列表框。
You must first get all relevant items from your list by using lambda and then try to use AddRange for adding the items to the listbox.