Reputation: 2634
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.
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?
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.
Upvotes: 0
Views: 1331
Reputation: 65564
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 a StackPanel
inside a ScrollViewer
and reuses the existing items, rather than creating new ones.
<ScrollViewer Height="629" Margin="0,139,0,0" Width="480">
<StackPanel Name="listBox1" />
</ScrollViewer>
cs:
private void InitializeResultsGrid()
{
...
for (int i = 0; i < 26; i++)
{
...
listBox1.Children.Add(new TextBlock());
and
private void SlowFill(string baseStr)
{
for (int i = 0; i < buttons.Count; i++)
{
(listBox1.Children[i] as TextBlock).Text = baseStr + (char)(i + 'a');
}
When I timed it, it was slightly slower than using the Grid but the performace seemed fine to me on an LG-E900
Upvotes: 4