Automatically open Search pane when typing in a Windows Store app

A great feature in Windows 8 is that you’re able to just start typing on the Start screen, which results in the Search pane opening and search being triggered with your input as search string. You can obtain the same behavior in your app by simply setting the ShowOnKeyboardInput flag of the SearchPane to true.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Windows.ApplicationModel.Search.SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;
}

The major drawback is that every single keystroke is directed to the search pane, even if your focus is in a TextBox. For that reason you should consider which on which screens you’d like to enable search by default and where you have to keep it out. Or you can enable/disable depending on whether you’re in an editable field by handling the GotFocus and LostFocus events. For these reasons, we’ve moved out enabling and disabling search to a little helper class.

using Windows.ApplicationModel.Search;

namespace AppCreativity.Common.Helpers
{
    public class Search
    {
        /// <summary>
        /// Automatically enables type to search
        /// 
        /// Enable type to search on your main page or pages containing content that the user is likely to search.
        /// Enable type to search on search result pages, in case the user wants to search again/
        /// Avoid enabling type to search on pages with input fields, as type to search will direct keyboard input 
        /// to the Search charm rather than the page's input fields.
        /// </summary>
        public static void EnableTypeToSearch()
        {
            SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;
        }

        /// <summary>
        /// Disable type to search
        /// </summary>
        public static void DisableTypeToSearch()
        {
            SearchPane.GetForCurrentView().ShowOnKeyboardInput = false;
        }
    }
}
Licensed under CC BY-NC-SA 4.0; code samples licensed under MIT.
comments powered by Disqus
Built with Hugo - Based on Theme Stack designed by Jimmy