views:

301

answers:

1

In working with MS Expression Blend, you can import sample data from an XML file to use to test your XAML and bindings in design mode. My data model is fully defined in XML using the Entity Framework. Is there any reasonable way to use that model, either directly or indirectly, as the source for the Blend sample data? It seems like this is a fairly obvious use case, but I haven't found any samples of how to do this. Is my only option to write a utility that serializes an instance of my EF classes to XML?

(I don't think I can use a link to the live datasource, because I'm using Silverlight w/ WCF, and my data model is the auto-generated proxy classes. If there's a way to hook Blend up to a WCF service, I have yet to find it -- though I'd be happy to get some pointers.)

A: 

I still don't have a great answer for this. I tried serializing some instances of my EF classes to XML using the DataContractSerializer:

DataContractSerializer serializer = new DataContractSerializer(typeof(Room));
using (SlideLincEntities ctx = new SlideLincEntities())
{
    Room roomWithMostSessions = ctx.Room
        .OrderByDescending(r => r.Sessions.Count)
        .FirstOrDefault();
    string fileName = ConfigurationSettings.AppSettings["outputFile"];
    Console.WriteLine("Writing data to file '{0}'", fileName);
    File.Delete(fileName);
    using (Stream fileStream = File.Open(fileName, FileMode.OpenOrCreate))
    {
        serializer.WriteObject(fileStream, roomWithMostSessions);
    }
}

Unfortunately, Blend can't read the generated XML: the DataContractSerializer uses XML ref notation, which apparently confuses Blend's simplistic XML deserializer.

I also tried generating sample data by hand (uggh) using XAML's object notation:

<rs:Room d:IsDataSource="True"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:rs="clr-namespace:SlideLinc.Client.Common.RoomService;assembly=SlideLinc.Client.Common"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
Name="_default" OwnerId="smithkl42"
>
    <rs:Room.Owner>
        <rs:RegisteredUser x:Name="ken"  UserId="ken"  Email="[email protected]"  FirstName="Ken" LastName="Smith" MaxFileUploadSize="20000" UserName="Ken Smith" />
    </rs:Room.Owner>
</rs:Room>

This was better, but for some reason, Blend wouldn't bind to any collections within the objects: I don't know why, because of course XAML databinding errors get swallowed silently. (Have I mentioned how much I hate XAML databinding?)

I eventually went with defining small amounts of data straight within the XAML form proper:

<ListBox x:Name="fileListBox" Grid.Row="2" Margin="4" BorderThickness="0" >
 <ListBox.ItemTemplate>
  <DataTemplate>
   <Grid >
    <Grid.ColumnDefinitions>
     <ColumnDefinition Width="25" />
     <ColumnDefinition Width="150"/>
     <ColumnDefinition Width="70" />
     <ColumnDefinition Width="5" />
     <ColumnDefinition Width="30" />
    </Grid.ColumnDefinitions>
    <Image Source="/Images/Word.png" Grid.Column="0" Margin="2" />
    <TextBlock Text="{Binding OriginalFileName}" Grid.Column="1" VerticalAlignment="Center" />
    <HyperlinkButton Content="Share"  Grid.Column="2" Margin="4"  HorizontalAlignment="Right" VerticalAlignment="Center"/>
    <HyperlinkButton Grid.Column="4" Margin="4" HorizontalAlignment="Right" VerticalAlignment="Center" >
     <Image Source="/Images/trashcan.png" Width="25" />
    </HyperlinkButton>
   </Grid>
  </DataTemplate>
 </ListBox.ItemTemplate>
 <!-- I shouldn't have to do this, but for some reason, I can't get sample data any other way -->
 <roomservice:SharedFile OriginalFileName="Some Document.docx" />
 <roomservice:SharedFile OriginalFileName="Another document.pptx"/>
</ListBox>

So that's my current approach. Not very elegant. Open to other ideas.

Ken Smith