22792

How can I use DesignData to help develop a Metro app?

Question:

I've been merrily using the DesignData in Windows Phone apps, and I was hoping to use it to help visualise designs in a Metro style app in VS2012/Blend for VS.

I've tried the hopefully obvious:

<common:LayoutAwarePage x:Name="pageRoot" x:Class="MyRootNamespace.Views.EventView" DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}" IsTabStop="false" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MyRootNamespace.Views" xmlns:common="using:MyRootNamespace.Common" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:bm="using:Bing.Maps" d:DataContext="{d:DesignData Source=../SampleData/SpecialEventSampleData.xaml}" mc:Ignorable="d">

With the SpecialEventSampleData.xaml looking loosely like:

<local:SpecialEvent xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MyRootNamespace.ViewModels" EventName="Foo Fighters" Description="This exclusive April Fools Gig will see Dave Grohl and the rest of the Foo Fighters rock out at this exclusive made up gig" VenueName="Village Hall" />

The class to which it refers has a public, parameterless constructor, but Visual Studio is giving an error:

<blockquote>

Error Cannot create an instance of "SpecialEvent". C:...\MyRootNamespace.Windows\SampleData\SpecialEventSampleData.xaml

</blockquote>

In addition, I was expecting to have to set the build action to "DesignData", or similar, but this doesn't appear to be an option in Visual Studio 2012?

What do I need to do to be able to get <a href="http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh758320.aspx#displaying_data_in_the_designer" rel="nofollow">design time data</a> to work in VS2012/Blend?

Answer1:

Is SpecialEvent your ViewModel? If so, can you do something like this in your XAML (directly after your common:LayoutAwarePage opening declaration)?

<d:Page.DataContext> <local:SpecialEvent> </d:Page.DataContext>

And then remove this line from your common:LayoutAwarePage:

d:DataContext="{d:DesignData Source=../SampleData/SpecialEventSampleData.xaml}"

I have code similar to this that is working.

Answer2:

In Windows Phone (I believe) the path to the sample data file is relative to the application root. In Windows 8, the path to the sample data file is relative to the <em>page xaml file</em> you want to use it in.

In your sample above, the <em>page.xaml</em> file would need to be in a sub-folder of the project but in a different folder than <strong>SampleData</strong>.

Windows 8 sample data does not automatically create collection instances for you. So, if you have sample data like this:

<Event> <Attendees> <Attendee Name="Joe" /> </Attendees> </Event>

you would need to make sure that the backing field for the <strong>Attendees</strong> property is set to a new collection instance when you define the variable. If you are using automatic properties (<strong>Attendees { get; set; }</strong> syntax) you will need to set the property to a new collection instance in the constructor before items can be added in xaml.

Finaly, Windows 8 xaml cannot handle generic types. So, if your <strong>Attendees</strong> property is of type <strong>Collection<Attendee></strong> you will not be able to create an instance of it in xaml.

You can always make a simple class called <strong>AttendeeCollection</strong> that inherits from <strong>Collection<Attendee></strong> and create instances of that in your xaml instead.

Hope that helps.

Answer3:

Try full path to the xaml file instead of relative.

Answer4:

This is a touch obscure, but it boils down to the implementation I had of INotifyPropertyChanged on my View-Model.

In my Windows Phone code, I have to go to a small amount of effort to ensure that the events raised by background worker threads pop out on the UI thread:

protected delegate void OnUIThreadDelegate(); protected void OnUIThread(OnUIThreadDelegate onUIThreadDelegate) { if (Deployment.Current.Dispatcher.CheckAccess()) { onUIThreadDelegate(); } else { Deployment.Current.Dispatcher.BeginInvoke(onUIThreadDelegate); } }

This was then used:

protected override void OnPropertyChanged(PropertyChangedEventArgs e) { OnUIThread(() => { base.OnPropertyChanged(e); }); }

It appears that when porting that code, I was not checking the result of CoreWindow.GetForCurrentThread() which was causing an exception to fire deep in the darkest depths of the code, as the designer was setting the properties on my type, and I was trying to raise the events and dying horribly instead. My bad.

Recommend

  • Use dictionary to replace a string within a string in Pandas columns
  • Detect hitting Enter Key in C++
  • Order of join conditions important?
  • SuperCSV append rather than overwrite?
  • How to use commands only when a current command is triggered?
  • Element naming convention in Razor generator within a foreach loop
  • What does the ~ in front of the SASS/SCSS @import mean/do?
  • Binding a property defined in code-behind, and another one defined in a class in the same template
  • Python - How to pass instance variable value to descriptor parameter?
  • C# / Object oriented design - maintaining valid object state
  • Reference to an object from a callback function in jQuery
  • Typescript - How to check if a given value is in a union type array
  • Where are value types defined in a reference type stored (heap or stack)?
  • ES6 calling super() doesn't properly initialize parent class
  • How can I shrink a postgresql database?
  • Python does not find System32
  • SKSpriteKit, detect non-transparency parts
  • Couple of questions on mysql table creation
  • interesting behavior of calling method after casting a subclass to a super class
  • Is the lifetime of an exception affected by other exceptions?
  • Tool for creating preview images from equirectangular Panoramas
  • Dynamic URLs for a Liferay Portlet
  • Javascript bubble sort of an array with objects
  • Select document having particular key value pair but not having other key value pair
  • Difference between a changeset and a patch?
  • Send http request through specific network interface
  • Dynamically generated lookup key for IQueryable
  • Angular2 & SystemJS : Cannot find module while building a moduleLoader
  • JsonConverter: Get parent object in ReadJson, without $refs
  • Obtain actual browser URL in PHP
  • How does inheritance and polymorphism work in this situation?
  • What does “t” refer to in this SQL?
  • Authentication in Play! and RestEasy
  • jQuery ready not fired after rails link_to is clicked
  • Bad request using file_get_contents for PUT request in PHP
  • Refering to the class itself from within a class mehod in Objective C
  • Matrix multiplication with MKL
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app