If you have been paying attention to any of the Windows 8/Metro stuff that has come out sense BUILD you may have noticed that with Windows 8/Metro application developers can now easily light up their applications by enabling their apps to hook into the system search mechanism. This post (and subsequent ones) will walk though how to enable this in your application.
Window 8 has enabled developers to hook into various system ‘Contracts’ such as Search, Sharing and Settings. Contracts are like agreements between Windows and one or more Metro style apps, called contract participants, to support some kind of user interaction. There is a contract for each type of interaction, like playing music from an application to a connected stereo, and the contract specifies the types of capabilities the participant has.
Enabling Search
In this post we will take a look at how your application can provide context sensitive suggestions to the search panel. Adding these suggestions can provide your
user the ability to not have to type the entire search term in order to perform a search. It also provides a list of known keywords that your application can use which by default provides your user a higher level of confidence that the keyword they are searching on will return data.
How do we setup the ability to provide keywords to the Search Panel?
Step 1: Get the instance of the Current Search Pane
- Add a reference to the Windows.ApplicationModel.Search Namespace.
- Grab the instance of the pane by using the following syntax
SearchPane.GetForCurrentView() - Store the instance of this pane as a global variable/property
Step 2: Hookup your applications to the correct eventyo
- Using the Pane instance you gathered in step 1, wire up the following event
- SuggestionsRequested
Step 3: Build a list of keywords to provide as suggestions
In my code base I have a repository method which will parse a list of keywords and return them as a List<string>. One thing to pay attention to is that your string can only contain ONE WORD. I have tried this with multiple words and it does not appear to work. I don’t know if this is a Developer Preview limitation or if this is intended behavior but either way only put a single word per line in your search result list
Step 4: Handle the SuggestionsRequested Event callback
In the handler of your SuggestionsRequested event you need to push your list of suggestion keywords into the Requested args as follows
args.Request.SearchSuggestionCollection.AppendQuerySuggestions(suggestions);
Once you have pushed your list of suggestions into the SearchSuggestionCollection you should now see your suggestions in the search panel when searching on your application.
Till next time,
Posted
09-26-2011 5:40 PM
by
Derik Whittaker