Displaying design-time data in a C++/CX app
Previously I’ve written about detecting design mode in Blend, specifically in a WinJS app. This is sometimes necessary if you want to enable the designer-developer workflow. The designer-developer workflow means that during the app development process, designers and developers can work independently and concurrently on their components. The designers can concentrate on the UI in Blend, displaying sample data to view layout sizes accurately and see realistic results for sizing and styling, while the developers can work on the code in Visual Studio.
In most circumstances for HTML5 apps, Blend can automatically get data from the file system without any additional effort from the developer, although it’s occasionally necessary to add logic to check for design mode. However, more work is required to display data from the file system at design-time in a C++/CX app.
In a previous blog post, I extended a sample photo viewing app to use the Repository pattern so that access to the underlying data source, in this case the file system, is through a centralized data access layer. In this blog post I will add design-time data to the app, in order to enable the designer-developer workflow.
The sample application can be downloaded here.
Implementation
To display data in the designer, you must declare it in XAML instead of just setting the DataContext property programmatically in code-behind. This is necessary because when you open a page or user control in the designer, it is not instantiated. The designer parses the XAML, but does not run its code-behind. However, the designer will instantiate any child user controls that it encounters when it parses the XAML content.
The technique adopted here for displaying design-time data is to use design-time attributes in page’s XAML. To do this, you must ensure that the designer namespace is declared in your XAML file.
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
namespace PhotoViewer
{
[Windows::UI::Xaml::Data::Bindable]
public ref class DesignTimeData sealed
{
public:
property Windows::UI::Xaml::Media::ImageSource^ Photo
{
Windows::UI::Xaml::Media::ImageSource^ get();
}
};
}
ImageSource^ DesignTimeData::Photo::get()
{
return ref new BitmapImage(ref new Uri("ms-appx:///Assets/Logo.png"));
}
<Grid x:Name="ContentRoot"
d:DataContext="{Binding Source={d:DesignInstance Type=local:DesignTimeData, IsDesignTimeCreatable=True}}"
Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
<Image HorizontalAlignment="Center"
Source="{Binding Photo}"
VerticalAlignment="Center" />

The image shown in the screenshot above is simply the default logo (Logo.png) for a Windows store app project.
For a more complicated implementation of design-time data, which includes grouped data, see the Hilo project.
Summary
Design-time data can be added to Windows Store apps written in C++/CX, in order to enable the designer-developer workflow. The designer-developer workflow means that during the app development process, designers and developers can work independently and concurrently on their components. The designers can concentrate on the UI in Blend, displaying sample data to view layout sizes accurately and see realistic results for sizing and styling, while the developers can work on the code in Visual Studio.