DVLUP
July 2, 2014

App Studio - Design-Time Sample Data

Posted on July 2, 2014  •  6 minutes  • 1184 words
Table of contents

If you’re like me, you like to fiddle and tweak things to get just the right result . Recently, Microsoft has really beefed up App Studio apps and if you explore the downloaded source code you’ll see a new Universal app structure leveraging MVVM. Let’s start with the before and after photo (click any image to view larger):

[caption id=“attachment_1119” align=“alignnone” width=“402”]<img src="http://nokiawpdev.files.wordpress.com/2014/07/before-after-blog.png" alt="The image on the left is the UI before changes and the one on the right is afterwards"> Before and after changes to the items lists[/caption]

Here is a screenshot of the App Studio solution structure in the Visual Studio Solution Explorer (since AppStudio.Data is a Portable Class Library, you’ll need Visual Studio 2013 Pro [Update 2] or higher. See Tim&rsquo;s answer for a workaround):

<img src="http://nokiawpdev.files.wordpress.com/2014/07/blog1.png" alt="Blog1">

So, you’ve got yourself an App Studio app and want to make custom changes, this can be challenging because you won’t see anything in the designer because the data is not generated until runtime.  You’ll have to make educated guesses by using margins of the UI elements.  We can make this much easier this by creating design-time sample data.

This blog post is a tutorial on how to do just that. Keep in mind that even though I have the YouTube schema through out the app, it should work the same regardless of your app’s structure. Let’s get started…

Step One - Download and open the source code in Visual Studio 2013

You can find the source code’s download link on the same page you downloaded the publish package. By default, it is collapsed, just expand the “Source Code” section and click the “Download 8.1 Source Code” link. It is a zipped folder, unblock and then extract it. Once the app is open in Visual Studio 2013, Build (F6) the solution so that the nuget packages are retrieved and added.

Step Two - Generate Sample Data

Now let’s open the project in Blend for Visual Studio (Pro Tip: right-click on AppStudio.WindowsPhone/Views/MainPage.xaml and select “Open in Blend") so that we can generate sample data for MainViewModel.

[caption id=“attachment_1098” align=“alignnone” width=“700”]<img src="http://nokiawpdev.files.wordpress.com/2014/07/blog2.png?w=700" alt="Click to enlarge"> Set Design-Time DataContext[/caption]

<img src="http://nokiawpdev.files.wordpress.com/2014/07/blog3.png" alt="Blog3">

Now your MainPage’s design-time DataContext is set to the sample data. Next, we’ll edit the sample data with some image paths so that you can see an images in the designer.

Step Three - Images

One downside to automatically generated sample data is that sometimes you want a path to an image instead of Lorem Ipsum. This is very easy fix that makes life much easier in the designer because Image controls bound to that sample data’s property will show an image, here are the steps:

<img src="http://nokiawpdev.files.wordpress.com/2014/07/blog4.png?w=700" alt="Blog4">

Step Four - Setting d:DataContext

Now we need to set the design data context of your view items to point to the sample data. We already did this at the page level in step two, but we’ll need to set it for each HubItem to display the lists. Here are the steps:

<img src="http://nokiawpdev.files.wordpress.com/2014/07/blog5.png?w=700" alt="Blog5">

Now, add a d:DataContext="{Binding ModelNameHere}" to each HubSection. You will find the names in the MainViewModelSampleData tree. In my demo, there are 5 HubSections, each has it’s own model. (App Studio models are outside the scope of this article, but you can read more here on the App Studio website ).

This screenshot explains it more clearly:

<img src="http://nokiawpdev.files.wordpress.com/2014/07/blog6.png?w=700" alt="Blog6">

That’s it! You should now be seeing your images and sample data in the designer.

Step 5 - Editing DataTemplates

This is the same process as you do for editing any Template. I wanted to quickly cover it to round out this tutorial. I wanted to get rid of the extra space between items in the list because I don’t have the summary text. Here is what I did:

Now let’s edit the ItemTemplate of the ListView that shows the list items:

  1. Right click on ListView
  2. Expand “Edit Additional Templates
  3. Expand “Edit Generated Items (Item Template)
  4. Select “Edit Current

You can now edit the template, I removed the second Grid.Row declaration because it wasn’t being used. I got back 60 pixels on unneeded padding, but remember empty space is just as important as content. So, I added a 12 px margin to the bottom of the Image control. This now allows more items to appear on the smaller phone form factor.

Conclusion

With a little sample data and some independent flavoring, you can customize your App Studio app to give it a truly unique feel. You can see the results of having design-time data right now, here is the app I used in this demo, enjoy!

Here is the Windows Phone version of the customized app

Here is the Windows version of the customized app

Now, go forth and design away!

Follow me on your preferred social network =>