Tuesday, June 14, 2011

Autocompletebox with long lists for windows phone 7

I recently was working with a list of American cities which has around 19000 cities in WP7 application. I wanted to use the autocompletebox which will be helpful to find a name fast. However, the autocompletebox was sluggish. Hence I created a list of lists. The lists where for each alphabet which reduced the list sizes to reasonable size. I also used LINQ to get the city name.

When I tested in WP7 I got all searches under 100 milliseconds and most of them around 2 to 30 milliseconds.

Another thing I found out is the autocompletebox does not like Pivot control. The selection list offsets to the top of the screen with the first selection completely masked!

See the reference below

http://www.jeff.wilcox.name/2011/03/acb-in-pivot/

The XAML and the code are given below. This also used the textbox substitution to make sure the initial display did not have the drop down of the autocompletebox as per this forum discussion which I initiated:

AutoCompleteBox initial value in Windows Phone 7

 <StackPanel Grid.Row="0" Orientation="Horizontal"   HorizontalAlignment="Stretch">
<toolkit:AutoCompleteBox ItemsSource="{Binding}" x:Name="City_Name_TextBox" ValueMemberPath="CityName"
Width="370" Visibility="Collapsed" MinimumPopulateDelay="200" <toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate >
<StackPanel VerticalAlignment="Stretch" >
<TextBlock Text="{Binding CityName}" />
</StackPanel>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
<TextBox Width="300" x:Name="City_Name_TextBox_Init" Visibility="Visible" Text="Palo Alto" MouseLeftButtonDown="City_Name_TextBox_Init_MouseLeftButtonDown"></TextBox>
<Button x:Name="Go_Button" Width="100" Content="GO" Click="Go_Button_Click" ></Button>
</StackPanel>
   
 private void City_Name_TextBox_Init_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
City_Name_TextBox_Init.Visibility = System.Windows.Visibility.Collapsed;
City_Name_TextBox.Visibility = System.Windows.Visibility.Visible;
City_Name_TextBox.Text = "";
City_Name_TextBox.Focus();
// City_Name_TextBox.Focus();
}
 
 public void Load_City_Alpha()
{
string[] abclist = new[] { "a", "b", "c","d","e","f","g","h","i","j",
"k","l","m","n","o","p","q","r","s","t",
"u","v","w","x","y","z"};
int salphaselectormax=26;
DateTime dtstart = DateTime.Now ;

for ( int salphaselector = 0; salphaselector < salphaselectormax; salphaselector++)
{
string salpha = abclist[salphaselector];
city_list_abc.Add( (from n in citylist
where n.CityName.ToLower().StartsWith(salpha)
select n).ToList());


}
//List<string> testlistlength = new List<string>();
//for (int i = 0; i < 26; i++)
//{
// testlistlength.Add(abclist[i]+"--" + city_list_abc[i].Count.ToString());
//}
DateTime dtend = DateTime.Now;
int dtduration = (dtend - dtstart).Milliseconds;

}
  private void City_Name_TextBox_Populating(object sender, PopulatingEventArgs e)
{
// Allow us to wait for the response
DateTime dtstart = DateTime.Now;
e.Cancel = true;
//only query web service if more than 3 chars

if (City_Name_TextBox.Text.Length >= 3)
{
// Create a request for suggestion
int listindex = Array.IndexOf(abclist, City_Name_TextBox.Text.ToLower().Substring(0,1));
List<City> results = (from n in (Application.Current as App).city_list_abc[listindex]
where n.CityName.ToLower().StartsWith(City_Name_TextBox.Text.ToLower())
select n).ToList();
City_Name_TextBox.ItemsSource = results;
City_Name_TextBox.PopulateComplete();
}
DateTime dtend = DateTime.Now;
int dtduration = (dtend - dtstart).Milliseconds;
Timer_TextBox.Text = dtduration.ToString();
}







1 comment:

  1. Rize provides Professional Windows Phone Development and management services to deploy and build customized Mobile Phone Appls for Windows Phones.

    ReplyDelete