Microsoft Visual Studio offers a number of templates for building Windows 8 applications. In most of my postings, we’ve been using the Blank application template, but the Grid App template is extremely powerful. Unfortunately, it is not obvious how to adapt it to your own data and it doesn’t come with an instruction manual.
This posting will show you how to substitute your own data for the design-time data that comes out of the box. We will keep the design time data for, er, design time, and we’ll add “real” data (well, fake data) for run-time.
The first step is to fire up Visual Studio and create a new application, using the Grid App (XAML) template under Visual C# Windows Store, as shown in the figure. (Click on the figure to see it full size).
When Visual Studio settles down, run the application as is, right out of the box. You’ll see “design time data” displayed in three pages: the hub page, the Groups page and the Items page.
Our goal is to show our own data in these pages.
To do so, we first need to find where the design time data is created. Open the DataModel folder and navigate to the constructor for the Sample Data Source. A simple way to do so is to expand the DataModel in the solution Explorer, then to expand the SampleDataSource and to click on the constructor. That will cause the editing window to navigate directly to the top of the method.
Enclose the entire contents of the method in the following if statement:
Before we can put the code we want into the else statement, we need to create the data classes we need. In the DataModel add a People class,
At this point we would normally call out to a web service or into a database, but instead we’ll randomly generate the data we need through a class that we’ll create called PeopleService, (also added to the DataModel folder),
We’re now ready to fill in the code back in SampleDataSource,
The result is a hub page with the people grouped by cities, a group page for each city and a detail page for each person.
[This posting is based in large part on material to appear in Pro Windows 8 Programming with C# and XAML by Jesse Liberty and Jon Galloway, APress 2013 ]
Download the Source Code.
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
Subscribe to be the first to get our expert-written articles and tutorials for developers!