Loading design time data from file into Blend in code in WinRT

250 views Asked by At

VS new project screenshot

If you create an out-of-the-box WinRT phone app in Visual Studio, e.g. a Hub App (Windows 8.1 Universal), the resulting XAML loads design time data in XAML like this:

<HubSection 
    IsHeaderInteractive="True" 
    DataContext="{Binding Section3Items}" 
    d:DataContext="{Binding Groups[3], Source={d:DesignData Source=../HubApp1.Shared/DataModel/SampleData.json, Type=data:SampleDataSource}}"
    x:Uid="Section3Header"
    Header="Section 3"
    Padding="40,40,40,32">

This design time data parsed in from a JSON file is visible in Blend:

Blend screenshot

I want to move the JSON file loading and parsing out of XAML and into C#, and have it still available in Blend. How do I do that? The challenge I face is that to be available in Blend I think the C# has to use a parameterless constructor. But loading a file (in WinRT) is asynchronous. Hence the method that opens the file must either be marked async and use await, or use a ContinueWith continuation. However async and thus await are not allowed in constructors and anything in a continuation appears to happen after the page loads into Blend and is not reflected in the Blend design view.

How do I load design time data, parsed from file, into Blend in code in WinRT?

(N.B. This is a generalisation of another question.)

1

There are 1 answers

0
Andrei Tătar On BEST ANSWER

You can always block the thread waiting for the task to finish using task.Wait() or task.Result (if you need the result).