Friday, October 16, 2009

Silverlight 3 XamlWriter - v0.4 - DataTemplate support

The new version of XamlWriter with DataTemplate support is now available. Check the previous posts for download and usage information.

While it is possible to recreate the DataTemplate it is not possible to recreate ContentTemplate. Hence I couldn't add support for ContentTemplate. There is also support for the Style class but there is an issue with it.

The DataTemplate example



Assuming an Xaml like this

<UserControl x:Class="test2.MainPage" x:Name="MyControl"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    Width="1000" Height="300">
    <Border BorderBrush="Green" BorderThickness="4">
        <Grid x:Name="LayoutRoot" Background="White"  >
            <controls:TreeView x:Name="trv" ItemsSource="{Binding}" Width="500" BorderThickness="4" BorderBrush="Purple">
                <controls:TreeView.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Blue" BorderThickness="4">
                            <Grid>
                                <Border BorderBrush="Yellow" BorderThickness="4">
                                    <TextBlock Text="Hello"></TextBlock>
                                </Border>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </controls:TreeView.ItemTemplate>
            </controls:TreeView>
            <Button x:Name="zz" Width="100" Height="100" Click="Button_Click"/>
        </Grid>
    </Border>
</UserControl>


WriteXaml(MyControl, XamlWriterSettings.LogicalTree) will generate this Xaml


<UserControl x:Name="MyControl" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/client/2007">
  <Border BorderThickness="4,4,4,4" BorderBrush="#FF008000">
    <Grid x:Name="LayoutRoot" Background="#FFFFFFFF">
      <TreeView BorderThickness="4,4,4,4" Width="500" x:Name="trv" BorderBrush="#FF800080">
        <TreeView.ItemTemplate>
          <DataTemplate>
            <Border BorderThickness="4,4,4,4" BorderBrush="#FF0000FF">
              <Grid>
                <Border BorderThickness="4,4,4,4" BorderBrush="#FFFFFF00">
                  <TextBlock Text="Hello" />
                </Border>
              </Grid>
            </Border>
          </DataTemplate>
        </TreeView.ItemTemplate>
      </TreeView>
      <Button Width="100" Height="100" x:Name="zz" />
    </Grid>
  </Border>
</UserControl>

12 comments:

test said...

Very good job but your assembly is not compatible with Visual Studio 2010 beta 2. Can you fix it please ?
thx

Wilfred Pinto said...

I will look into it as soon as I get access to a machine with Visual Studio 2010.

David Price said...

Fantastic. I'm using it to build a WYSIWYG editor for my project.

For some reason it doesn't output the TextDecorations property on the TextBlock element. Are you able to put this in the next version?

Everything else i've tried works perfectly, thanks again!

Anonymous said...

would be great, if it also support Path.Data...

but it is the best xamlwriter i have seen till now

Wilfred Pinto said...

David,

Thanks for the feedback. I will look into the TextDecorations property and Path.Data element as soon as I get some time.

Please continue to post the issues as and when you encounter them.

Thanks

Wilfred

gerfen said...

Hi Wilfred,

Thanks for providing the assembly. Unfortunately it is not working very well for me. When I try to serialize one of my controls, which is dynamically created at run time I get the following exception[1]. I'd love to send you an example but as I stated my control is built dynamically at run time. Any chance you could provide the source code so I can debug locally?

Thanks!

--Michael


[1] Exception.
Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.writeCLRProperties(DependencyObject target, DependencyObject targetBlank)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.WriteXaml(Object parent, DependencyObject parentBlank)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.WriteXaml(Object parent)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.writeCollections(DependencyObject target, DependencyObject targetBlank)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.WriteXaml(Object parent, DependencyObject parentBlank)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.WriteXaml(Object parent)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.writeCollections(DependencyObject target, DependencyObject targetBlank)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.WriteXaml(Object parent, DependencyObject parentBlank)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.WriteXaml(Object parent)
at projectsilverlight.blogspot.com.XamlWriter.XamlWriter.WriteXaml(Object parent, XamlWriterSettings xamlSettings)

Wilfred Pinto said...

Michael,

Send a email to my email address (is available in one of my posts) and I will email you the source code (if I can find it!)

Please send all modifications back so that I can incorporate it in the master.

Thanks

Wilfred

Chaos Land said...

Hey there,
First of all this is a very nice library!

I only have one issue and i am not sure if it is something i'm doing or a "known issue". So, using it directly in a Silverlight app, it works okay. When i moved it in a WCF service, absolutely in the same manner (code compiles okay, no warnings / errors), however when i test the service (...service.svc?wsdl) it throws error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'projectsilverlight' could not be found (are you missing a using directive or an assembly reference?)

Source Error:



Line 9: using System.Xml.Serialization;
Line 10: using System.IO;
Line 11: using projectsilverlight.blogspot.com.XamlWriter;
Line 12:
Line 13:

Beats me how it gets compiled in the first place. Let me know if you have any idea. Thanks!

Anonymous said...

Hi this message is just for reporting a bug,

when i use XamlWriter to export the string corresponding to a lineargradientbrush I noticied that startpoint and endpoint was not in correct format Example : StartPoint="0,5;0,5"

the silverlight xamlreader is expecting StartPoint="0.5 0.5" instead

If think this might be coming from the regional options (my computer is in French regional option) when i set the regional options to English (United States) the silverlight xamlreader throws no exception.

I made a fragile workaround to translate the string in a regional unsensitive format

Anyway thanks for this dll !!!

I'll be checking in the future if a corrective version cames out ...

Bye

Wilfred Pinto said...

I will post the source code soon so that you all can work around your issues. Unfortunately I do not have time to work on it.

Ray said...

Maybe move it to codeplex or google code so others can help contribute?

Microsoft Silverlight Applications said...

Your this post served a great purpose of mine.Assembly worked well for me apart from few inclusions and exclusions.Great work!!!