Back to Home

Search contract for Windows store apps

September 25, 20123 minutes read
Search contract for Windows store apps

A contract is like an agreement between one or more apps. Contracts define the requirements that apps must meet to participate in these unique Windows interactions.

For XAML Metro style apps, the Windows.UI.Xaml.Application class does a lot of the work needed for your app to support activation. This class exposes a set of strongly typed activation methods that you can override for supporting common contracts such as Search. For all contract activations that don’t have a strongly typed method, you can override the OnActivated method and inspect the activation kind to determine the contract for which your app is activated. New XAML app projects in Visual Studio come with generated code that uses the Windows.UI.Xaml.Application class to make the app capable of being activated for a generic launch.

There is an easier way than manually doing this work. You can use Visual Studio tooling for completing a lot of this work. Just right click on your project, select Add > New Item, and choose Search Contract in the dialog. Most of the code you see here, and a search UI that displays results in a way that follows our Search UX guidelines is automatically created for you.

To extend support for Search activation in your app:

      1. Add the Search declaration to your manifest using the Visual Studio Manifest Designer.
      2. Place in the App constructor of App.xaml.cs/cpp/vb any general initialization code that needs to run every time your application is started irrespective of the reason.
      3. Override the strongly typed OnSearchActivated method in App.xaml.cs/cpp/vb to handle search activation.
      4. Load your Search UI and show search results for the query you receive in the SearchActivatedEventArgs.

We must override the OnSearchActivated method to support activation for Search:

protected override void OnSearchActivated(SearchActivatedEventArgs args)
// Load Search UI

The Activate method of the SearchResultsPage sets up a UI that shows search results for the user’s search query:

        // SearchResultsPage.xaml.cs code snippet
public static void Activate(String queryText)
// If the window isn't already using Frame navigation, insert our own frame
var previousContent = Window.Current.Content;
var frame = previousContent as Frame;
if (frame == null)
frame = new Frame();
Window.Current.Content = frame;
// Use navigation to display the results, packing both the query text and the previous
// Window content into a single parameter object
new Tuple(queryText, previousContent));
// The window must be activated in 15 seconds