Windows 8–Let Templates Simplify the Search Contract

In a previous post, I showed a bit about how to implement the Search contract.  Once you understand the changes to Package.appxmanifest and App.xaml.cs, however, you can save yourself the bother of mucking about with these, and let the template that comes with Visual Studio fix up all the plumbing for you. 


All you need do is right click on the project and choose Search Contract.  If you take the default name, a page named SearchResultsPage1.xaml is created for you (and all the plumbing is put in place). 

The trick is that this page expects search results to be presented as objects with a Title, SubTitle, Description and Uri, which you can easily provide by creating a class; let’s call it SearchResult:

 sealed class SearchResult
    public Uri Image { get; set; }
    public string Title { get; set; }
    public string Subtitle { get; set; }
    public string Description { get; set; }


Open SearchResultPage1.xaml.cs and find the method Filter_SelectionChanged.  In here you’ll find a TODO marked off where youa re to set DefaultViewModel[“Results”] which expects an array of such objects,  You can easily hard code your first set of responses like this,

SearchResult[] myResults = new SearchResult[]
   new SearchResult { Title = "1", 
             Description="S1 Desc", 
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "2", 
             Description="S2 Desc", 
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "3", 
             Description="S3 Desc", 
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "4", 
             Description="S4 Desc", 
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "5", 
             Description="S5 Desc", 
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") }

The final step is to assign this array to the DefaultViewModel,

DefaultViewModel["Results"] = myResults;

The rest of the work is taken care of for you. If you run the program and open the search charm and execute a search (it doesn’t matter what you search for, the results are hard coded) you get the results shown in the figure above. 


Download the source code

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Data, Mini-Tutorial, Windows 8 and tagged . Bookmark the permalink.

4 Responses to Windows 8–Let Templates Simplify the Search Contract

  1. Oh my goodness! Amazing article dude! Thank you, However I am going through difficulties with your RSS.

    I don’t know why I cannot join it. Is there anybody having the
    same RSS problems? Anyone that knows the solution will you kindly respond?


  2. I’m amazed, I must say. Seldom do I come across a blog that’s both equally educative and engaging, and without a doubt, you’ve hit the nail on the head.

    The problem is an issue that too few people are speaking intelligently about.
    I am very happy I came across this in my hunt for something regarding this.

  3. test website says:

    Can I make a suggestion? I believe youve obtained something great here. But what need to you added a pair links to a page that backs up what youre saying? Or possibly you could give us one thing to appear at, one thing that may connect what youre saying to one thing tangible? Only a suggestion. Anyway, in my language, there aren’t much great source like this.

  4. Pingback: Windows Store Developer Links – 2013-02-20 | Dan Rigby

Comments are closed.