Question

I been watching TechDays 2010 Understanding MVVM and at one point he talks about blend and creating sample data but instead of generating it in blend he makes the data in C# code.

I am wondering if you create sample data(from sample class,new sample data and etc) does it save it somewhere in the project(ie I give my project to someone else will they see the same data in blend when they load up the project)? Can you switch from sample data and live data easily?

Was it helpful?

Solution

Sample data is just an xaml (not just xml) file defining your object graph that is marked with the build types DesignData or DesignDataWithDesignTimeCreatableTypes. The docs are sparse on MSDN, but this document about its use in the Silverlight designer is essentially the same in any xaml designer in 2012.

There is no "live data" when using these types of samples. All the values are set in the xaml file. You can't change the data for, say, a particular text box within the designer. Nor can you easily switch between different samples.

There are two ways to create the sample data--you can build it by hand (if you know your types and if you are comfortable writing xaml), or you can spin up a simple console application, build your object graph, then use the XamlServices class to serialize your graph to a string (or just rewrite to drop it to a stream instead). Here's some C# pseudocode that may or may not work as written:

public string Serialize(object toSerialize)
{
    var sb = new StringBuilder();
    var writer = XmlWriter.Create(sb);
    XamlServices.Save(writer, toSerialize);
    writer.Flush();
    writer.Close();
    return sb.ToString();
}

You just create a new file, give it an .xaml extension, drop the result in that file, save it to your solution, and set its Build Action to DesignData (the designer mocks the structure of your types) or DesignTimeDataWithDesignTimeCreatableTypes (the latter if your graph can be deserialized with XamlServices, doesn't throw any exceptions when used in the designer, etc).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top