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 episode we will look at how we can enable real time search integration. What do I mean by Real Time Search integration? When building a Metro Application you have the ability to not only handle unprovoked search queries from the operating system, but you also have the ability to hook into the search screen (seen below) but you also have the ability listen to various events that the Search Panel offers. By enabling your application to listen for these events you can do a few things like get the search query text as it is being typed. We can also handle the search request, this is different than the prior post because in this case your application is currently running.

How do we enable Real Time Search Integration?
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 events
- Using the Pane instance you gathered in step 1, wire up the following 2 events
- QueryChanged
- QuerySubmitted
Step 3: Handle the events and do something special
Now that we have the events wired up it is now up to you to do something special in the handlers. In my code base I don’t actually handle the QuerySubmitted event because I am doing filtering on the fly as the user is typing it.
If you want to get the current querystring value both events provide the following args.QueryText. The Query Text will give you the value of the search as it is being typed.
As you can see hooking your metro application to work with the search in real time is pretty much cake. However, even though it is easy it will light up your application and provide a very rich user experience for your users.
Till next time,
Posted
09-25-2011 5:28 PM
by
Derik Whittaker