1

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?

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • (psst, that's like 2 years old and stuff) –  Feb 20 '13 at 20:40
  • Yep I know. But eveytime I ask about MVVMLight I am referred to that and the 2011 video to reference...so guess it is still relevant. – chobo2 Feb 20 '13 at 22:15

1 Answers1

2

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).

  • In the video I linked the speaker decided to use C# as generating the data. Does that suffer from the same problems that it uses design data in the emulator(if using wp7) and while in blend but when you go to deploy it then it uses real data? – chobo2 Feb 20 '13 at 22:17
  • Design data only allows you to see data in the design surface. If you can, then you at least know that your bindings work. That (and tooling that helps generate the bindings) is the only benefit. Design data is no deployed. I can't speak to the vidya as I haven't watched it. –  Feb 20 '13 at 23:35