Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (161).NET Core  (29).NET MAUI  (65)Angular  (74)ASP.NET  (50)ASP.NET Core  (79)ASP.NET MVC  (88)Azure  (36)Blazor  (153)DocIO  (23)Essential JS 2  (86)Essential Studio  (174)File Formats  (29)Flutter  (123)JavaScript  (187)Microsoft  (113)PDF  (67)React  (62)Succinctly series  (128)Syncfusion  (612)TypeScript  (32)Uno Platform  (3)UWP  (4)Vue  (40)Webinar  (35)Windows Forms  (59)WinUI  (53)WPF  (138)Xamarin  (156)XlsIO  (25)Other CategoriesBarcode  (5)BI  (29)Bold BI  (5)Build conference  (6)Business intelligence  (54)Button  (4)C#  (120)Chart  (57)Cloud  (12)Company  (445)Dashboard  (8)Data Science  (3)Data Validation  (6)DataGrid  (55)Development  (403)Doc  (7)DockingManager  (1)eBook  (96)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (23)Extensions  (18)File Manager  (4)Gantt  (11)Gauge  (11)Git  (4)Grid  (28)HTML  (11)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (314)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (30)Performance  (9)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (7)Road Map  (10)Scheduler  (37)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (23)Solution Services  (3)Spreadsheet  (11)SQL  (8)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (92)UI  (226)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (23)Visual Studio Code  (17)Web  (377)What's new  (213)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Achieve Google-Like Autosuggestions with the WinUI AutoComplete Control

Achieve Google-Like Autosuggestions with the WinUI AutoComplete Control

The Syncfusion WinUI AutoComplete loads and populates data in a suggestion list depending on the user’s input characters. Users can then select one or more items from the list. Items selected from the list can be displayed with images, text, and close buttons to remove them as needed.

Key features

The key features of the WinUI AutoComplete control are as follows:

  • Smoothly and dynamically presents items based on the user input.
  • Does not include a dropdown button. When a user types, the dropdown automatically opens with suggestions.
  • Handles a huge volume of data and shows suggestions based on logical matching instead of just string comparison.
  • Also supports filtering, grouping, appearance customization, and more.

The best example of AutoComplete control is a search engine. When you search for something, suggestions are listed based on popular queries by other users.

In this blog, we will see how to achieve Google-like autosuggestions in a WinUI application using our AutoComplete control.

Google-like autosuggestions using the WinUI AutoComplete control

Let’s fetch suggestions from the Google search engine and show them using the WinUI AutoComplete control.

Google-Like Autosuggestions Using WinUI AutoComplete Control
Google-Like Autosuggestions Using WinUI AutoComplete Control

Step #1: Adding the WinUI AutoComplete control to your application

Follow these steps to add the AutoComplete control to the XAML page of your WinUI app:

  1. First, create a WinUI 3 desktop app for C# and .NET 5.
  2. Then, download and install the Syncfusion.Editors.WinUI NuGet package in the project.
  3. Now, import the control namespace Syncfusion.UI.Xaml.Editors on the XAML page.
  4. Initialize the WinUI AutoComplete control. Refer to the following code example.
    <Window
       x:Class="WinUI_Calendar.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:local="using:WinUI_Calendar "
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:editors="using:Syncfusion.UI.Xaml.Editors"
       mc:Ignorable="d">
     <Grid>
       <editors:SfAutoComplete Width="300"/>
     </Grid>
    </Window>

Step #2: Get suggestions from the Google search engine based on user input

When we search for something in a search engine, we will get suggestions based on our input characters. Let’s see how to fetch suggestions from the Google search engine.

Refer to the following code to fetch suggestions as an XML document. Here, the query refers to the user’s input.

string xmlSuggestions;
 
using (HttpClient client = new HttpClient())
{
  var searchQuery = "https://www.google.com/complete/search?output=toolbar&q=" + query;
  xmlSuggestions = client.GetStringAsync(searchQuery);
}

Let’s say we are trying to search for information about today’s weather, so we’ll use the partial input how is the wea. This will give us the following search query: https://www.google.com/complete/search?output=toolbar&q=how%20is%20the%20wea&gl=us&hl=en&pws=0&gws_rd=cr.

This query will give the following suggestions:

<toplevel>
 <CompleteSuggestion>
   <suggestion data="how is the weather today"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather tomorrow"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather going to be tomorrow"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather outside"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather in new york"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather in spanish"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather tonight"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather in chicago"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
   <suggestion data="how is the weather in florida"/>
 </CompleteSuggestion>
</toplevel>

Step #3: Parse the XML document and get the suggestions

With the suggestions in hand as an XML document, we need to parse it and get the required data. Once the data is collected, we return the accumulated data as a list.

Refer to the following code.

XDocument doc = XDocument.Parse(xmlSuggestions);
var suggestions = doc.Descendants("CompleteSuggestion")
  .Select(
    item => item.Element("suggestion").Attribute("data").Value);
return suggestions.ToList();

Step #4: Show suggestions using WinUI AutoComplete based on user input

Now that we know how to get suggestions from the Google search engine based on the user input characters. Let’s see how to implement this in the WinUI AutoComplete control.

When a user provides input in the AutoComplete control, the FilterBehavior property will populate the suggestions. It is of type IAutoCompleteFilterBehavior.

Create the CustomFiltering class to implement the IAutoCompleteFilterBehavior interface. The GetMatchingItemsAsync method will be called whenever a user starts typing. Then, the items that are returned by this method will be shown as suggestions in the dropdown.

Refer to the following code example.

public class CustomFiltering : IAutoCompleteFilterBehavior
{
  public Task<object> GetMatchingItemsAsync(SfAutoComplete source, AutoCompleteFilterInfo filterInfo)
  {
    var result = GetGoogleSuggestions(filterInfo.Text);
    return Task.FromResult((object)result);
  }
 
  private IList<string> GetGoogleSuggestions(string query)
  {
    // Get suggestions using the Google search query.
  }
}
<editors:SfAutoComplete HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        PlaceholderText="Search something"
                        TextSearchMode="Contains"
                        Width="300"&gt;
 <editors:SfAutoComplete.FilterBehavior>
   <local:CustomFiltering/>
 </editors:SfAutoComplete.FilterBehavior>
</editors:SfAutoComplete>
Google-Like Autosuggestions Using WinUI AutoComplete Control
Google-Like Autosuggestions Using WinUI AutoComplete Control

That’s all there is to it!

GitHub demo

You can download a working example of Google-like autosuggestions using WinUI AutoComplete control from GitHub.

Conclusion

Thanks for reading! In this blog post, we have seen how to implement Google-like autosuggestions using the Syncfusion WinUI AutoComplete control. For more details, refer to the WinUI AutoComplete documentation and demo app in the Windows Store. Try out the procedure in this blog post and leave your feedback in the comments section below.

For existing customers, the newest version of Essential Studio is available for download from the license and downloads page. If you are not yet a customer, you can try our 30-day free trial to check out the newest features.

You can also contact us through our support forumsfeedback portal, or support portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top