Understanding AutoSuggestBox In UWP

In this article, we will discuss how to implement the AutoSuggestBox in UWP.

In this article, we will discuss how to implement the AutoSuggestBox in UWP. We have discussed about the AppBar and CommandBar implementation in my previous article. You all must be aware, AutoSuggestBox is a default class on Windows.UI.Xaml.Controls package in Windows Runtime APIs.

Universal Windows Platform Series can be understood from the links, given below:

Background

AutoSuggestBox is a UI control and is used for the list of suggestions to select from the user input. We are able to customize the control and enable its search functionality in AutoSuggestBox. There are three important items such as Text Changed, Suggestion chosen, and Query submitted in AutoSuggestBox.

  • Text Changed - When the user enters text, it updates the suggestion list.
  • Suggestion chosen - When the user chooses a suggestion in the suggestion list, it updates the text box.
  • Query submitted - When the user submits a query, it shows the query results.

How to implement AutoSuggestBox and run UWP App

We are going to discuss how to implement AutoSuggestBox with the sample apps on UWP and show the demo in multiple devices. We will see the step by step guidelines for the UWP AutoSuggestBox app creation here.

Step 1

Open Visual Studio 2015. Go to file menu, point to new and click new project. New Project Window will open, you can select an installed template like “ Universal” under the Windows on Visual C# Template and select a Blank App (Universal Windows). Type Project Name UWPAutoSuggestBox. Choose the project location path and click the OK button.

UWPAutoSuggestBox

Now, you can see the UWPAutoSuggestBox project structure as shown in the screenshot, given below:

structure

Step 2: As you all know, we have discussed project folder structure and the files in my previous article.

Step 3: As you all know, we have a toolbox available in Visual Studio, which is available in AutoSuggestBox control in the screenshot, given below:

control

Now, you can see the auto generated code, as shown below:

  1. <AutoSuggestBox HorizontalAlignment="Left" VerticalAlignment="Top"/>  

Search.xaml

  1. <Page  
  2.     x:Class="UWPAutoSuggestBox.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:UWPAutoSuggestBox"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.     <Page.TopAppBar>  
  10.         <CommandBar ClosedDisplayMode="Minimal"  Background="#1FA2E1">  
  11.             
  12.         </CommandBar>  
  13.     </Page.TopAppBar>  
  14.     <Page.BottomAppBar>  
  15.         <CommandBar ClosedDisplayMode="Minimal" Background="#1FA2E1"></CommandBar>  
  16.     </Page.BottomAppBar>  
  17.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  18.         <Grid.ColumnDefinitions>  
  19.             <ColumnDefinition Width="2*"></ColumnDefinition>  
  20.         </Grid.ColumnDefinitions>  
  21.         <Grid.RowDefinitions>  
  22.             <RowDefinition Height="1*"></RowDefinition>  
  23.         </Grid.RowDefinitions>  
  24.         <StackPanel Grid.Column="0" Grid.Row="0">  
  25.           <AutoSuggestBox PlaceholderText="Search" Name="AutoSeggestBox" Padding="15 100 15 15" QueryIcon="Find" TextChanged="AutoSuggestBox_TextChanged" SuggestionChosen="AutoSuggestBox_SuggestionChosen" QuerySubmitted="AutoSeggestBox_QuerySubmitted"/>  
  26.         </StackPanel>  
  27.                  
  28.     </Grid>  
  29. </Page>  

Search.xaml.cs

  1. using Windows.UI.Xaml.Controls;  
  2. using System.Collections.Generic;  
  3. using System;  
  4. using System.Linq;  
  5.   
  6. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409  
  7.   
  8. namespace UWPAutoSuggestBox  
  9. {  
  10.     /// <summary>  
  11.     /// An empty page that can be used on its own or navigated to within a Frame.  
  12.     /// </summary>  
  13.     public sealed partial class MainPage : Page  
  14.     {  
  15.         public MainPage()  
  16.         {  
  17.             this.InitializeComponent();  
  18.         }  
  19.         List<string> _listSuggestion = null;  
  20.         private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)  
  21.         {  
  22.             if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)  
  23.             {  
  24.                     List<string> _nameList = new List<string>();          
  25.                     _nameList.Add("Santhakumar");  
  26.                     _nameList.Add("Vishanth");  
  27.                     _nameList.Add("Dinesh");  
  28.                     _nameList.Add("Srinivasan");  
  29.                     _nameList.Add("Nandhakumar");  
  30.                    _listSuggestion = _nameList.Where(x => x.StartsWith(sender.Text)).ToList();  
  31.                     sender.ItemsSource = _listSuggestion;                 
  32.             }  
  33.         }  
  34.          
  35.         private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)  
  36.         {  
  37.             var selectedItem = args.SelectedItem.ToString();  
  38.             sender.Text = selectedItem;  
  39.         }  
  40.   
  41.         private void AutoSeggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)  
  42.         {  
  43.             if (args.ChosenSuggestion != null)  
  44.             {  
  45.                 AutoSeggestBox.Text = args.ChosenSuggestion.ToString();  
  46.             }  
  47.   
  48.         }  
  49.     }  
  50. }  

Step 4: Now, if you can run the UWPAutoSuggestBox Apps with the different devices, you can see how the apps looks, as shown below:

Select a Debug and Mobile Emulator 10.0.10586.0 WVGA4 inch 512 MB option to run the apps.

Emulator
 
Select Debug and Local Machine option to run the apps.
 
option

option

Step 5

Now, let's start to run the existing Universal Windows app in HoloLens Emulator. If you run the UWP Hello World Apps with the HoloLens Emulators, you can see how the apps look, as shown below:

Select a Debug and HoloLens Emulator 10.0.11082.1039 option to run the apps.

output 
 
Source Code 
 
You can also download the source code here.
 
Note

You can also read this article in my blog here.

Conclusion

I hope you understood AutoSuggestBox control and how to run it on multiple devices. I have covered all the required things. If you find anything which I missed in this article, please let me know. Please share your valuable feedback or suggestions.