I’m writing a simple dictionary app which gives suggestions for words as you type. The suggestions are displayed in a ListBox and each time the query changes, about 10 suggestions should appear.
Unfortunately, performance is low at the moment. It takes almost a second for the results to appear and I don’t understand why. EQATEC Profiler shows that my methods are running smoothly. I’ve confirmed this by putting a Stopwatch around my code. I’ve also experimented with the number of suggestions, and performance does increase with fewer items.
This leads me to conclude that rendering the ListBox (which I presume happens outside of my methods) is to blame for the lack of performance.
- Does rendering 10 items in a ListBox really take more than 250ms?
- How can I quickly put a small number of words on the screen?
Edit:
The way I fill my ListBox is very straightforward. Is it the right way?
resultsListBox.Items.Clear();
foreach (string s in suggestions.Words)
{
resultsListBox.Items.Add(s);
}
resultsListBox.SelectedIndex = suggestions.MatchIndex;
What you see here is really it: default ListBox, String items, no templates. Do I violate one of these principals?
- Ensure you have the item data template in a fixed sized container (grid).
- Avoid/remove using complex converters, when the same information can be easily provided by the data object.
- Avoid/remove nested structures, example listbox in a listbox item.
- Strongly recommended to not use user control inside the data template.
- Avoid/remove custom controls from the data template
The link below contains a demonstration of the ListBox performance in a simple project.
The project also shows an alternative (faster) way to display a list, using a Grid with Buttons. This list is not scrollable and therefore not a real solution.
http://www.mediafire.com/?jypcfm4cs3nvo5c
Remember to run the project on a device, because the emulator has very different performance. I’ve tested it on the Samsung Omnia 7.
It sounds like you’re creating your own AutoCompleteBox. Is there a specific reason for not using the one in the Toolkit?
I would expect the time taken to update the listbox to be dependent upon: how you’re updating it; the complexity of the listbox; and whatever-else is on the page.
In that you haven’t provided details about any of these it could be possible that it will take this long.
Edit
As an alternative to the
AutoCompleteBox(In theory you shouldn’t need to scroll the results of this–just enter more characters to filter further.) I’ve done some experimentation and the following seems to work best. It uses aStackPanelinside aScrollViewerand reuses the existing items, rather than creating new ones.cs:
and
When I timed it, it was slightly slower than using the Grid but the performace seemed fine to me on an LG-E900