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. 

SearchResults

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 you are 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",
             Subtitle="S1",
             Description="S1 Desc",
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "2",
             Subtitle="S2",
             Description="S2 Desc",
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "3",
             Subtitle="S3",
             Description="S3 Desc",
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "4",
             Subtitle="S4",
             Description="S4 Desc",
             Image=new Uri("ms-appx:///Assets/SmallLogo.png") },
   new SearchResult { Title = "5",
             Subtitle="S5",
             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


Win8_Download (2)


jesseLiberty
About the Author

Jesse Liberty

 has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog or follow him on twitter

Comments

Comments are disabled in preview mode.