Wednesday, October 14, 2009

Silverlight 3 XamlWriter - v0.2

I was just going through the Silverlight.net forums to see if there was any interesting question to answer and I came across a post which had some interesting Xaml content. This used the Accordian control and looked like a good test case so I copied the Xaml to my project and invoked XamlWriter on it.

And...it did not work! That was expected though. I would be very surprised if it did work.

So, back to the drawing board - a few fixes here, a few fixes there, discovered a few more bugs, fixed them, and stopped when I got it to work with the second Xaml test case.

Get the newer version of XamlWriter from here - XamlWriter.dll

The second example



Assuming an Xaml like this

<UserControl x:Class="test2.MainPage" x:Name="MyControl"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:layoutToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit"
       Height="313" Width="301">

    <Grid x:Name="LayoutRoot">
        <Canvas x:Name="Layer_1">
            <layoutToolkit:Accordion ExpandDirection="Down" SelectionMode="One" SelectionSequence="Simultaneous" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="297" Height="225">
                <layoutToolkit:AccordionItem Height="100">
                    <layoutToolkit:AccordionItem.Header >
                        <Canvas HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBlock Text="Bedrijfspotentieel" Canvas.Left="56.666" Canvas.Top="-12" ></TextBlock>
                        </Canvas>
                    </layoutToolkit:AccordionItem.Header>
                    <StackPanel>
                        <Button Width="75" Content="B1" Click="Button_Click"/>
                        <Button Width="75" Content="Button"/>
                    </StackPanel>
                </layoutToolkit:AccordionItem>
                <layoutToolkit:AccordionItem>
                    <layoutToolkit:AccordionItem.Header>
                        <Canvas>
                            <TextBlock Text="Bedrijfspotentieel"/>
                        </Canvas>
                    </layoutToolkit:AccordionItem.Header>
                    <Grid/>
                </layoutToolkit:AccordionItem>
            </layoutToolkit:Accordion>
        </Canvas>
    </Grid>
</UserControl>


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


<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="MyControl">
  <Grid x:Name="LayoutRoot">
    <Canvas x:Name="Layer_1">
      <Accordion Width="297" Height="225" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <AccordionItem Height="100">
          <AccordionItem.Header>
            <Canvas HorizontalAlignment="Center" VerticalAlignment="Center" UseLayoutRounding="False">
              <TextBlock Text="Bedrijfspotentieel" UseLayoutRounding="False" Canvas.Left="56.6660003662109" Canvas.Top="-12">
                <Run Text="Bedrijfspotentieel" />
              </TextBlock>
            </Canvas>
          </AccordionItem.Header>
          <AccordionItem.Content>
            <StackPanel>
              <Button Content="B1" Width="75" />
              <Button Content="Button" Width="75" />
            </StackPanel>
          </AccordionItem.Content>
        </AccordionItem>
        <AccordionItem>
          <AccordionItem.Header>
            <Canvas UseLayoutRounding="False">
              <TextBlock Text="Bedrijfspotentieel" UseLayoutRounding="False">
                <Run Text="Bedrijfspotentieel" />
              </TextBlock>
            </Canvas>
          </AccordionItem.Header>
        </AccordionItem>
      </Accordion>
    </Canvas>
  </Grid>
</UserControl>

No comments: