Silverlight's XamlWriter!

Hi,

Oh god!, this is my first blog post and this is the second time that I write a complete post and then IE crashes :((

nowadays we hear the sounds of the people who is looking around for a Silverlight's XamlWriter, but there's nothing!

I'm one of the guys who is contributing in Silverlight's Forums (rambler.elf) and I see so many people requested it.

I've decide to create a XamlWriter, and after about 10 hours working it's here! (no no, it's not a ideal XamlWriter, but don't worry, it's not so bad :-) )

a sample Xaml which is created at runtime by XamlWriter :

<Canvas Width="100" Height="150" Opacity="1" RenderTransformOrigin="0,0" Cursor="Default" IsHitTestVisible="True" Visibility="Visible" x:Name="test" Canvas.Left="20" Canvas.Top="0" Canvas.ZIndex="0">
  <Canvas.Background>
    <RadialGradientBrush Center="0.5,0.5" GradientOrigin="0.5,0.5" RadiusX="0.5" RadiusY="0.5" SpreadMethod="Pad" MappingMode="RelativeToBoundingBox" ColorInterpolationMode="SRgbLinearInterpolation" Opacity="1">
      <GradientStop Color="#FFFFFFFF" Offset="0.5" />
      <RadialGradientBrush.Transform>
        <TransformGroup>
          <RotateTransform CenterX="0" CenterY="0" Angle="80" />
        </TransformGroup>
      </RadialGradientBrush.Transform>
    </RadialGradientBrush>
  </Canvas.Background>
  <Canvas.OpacityMask>
    <RadialGradientBrush Center="0.5,0.5" GradientOrigin="0.5,0.5" RadiusX="0.5" RadiusY="0.5" SpreadMethod="Pad" MappingMode="RelativeToBoundingBox" ColorInterpolationMode="SRgbLinearInterpolation" Opacity="1">
      <GradientStop Color="#FFFFFFFF" Offset="0.5" />
    </RadialGradientBrush>
  </Canvas.OpacityMask>
 
    <Ellipse StrokeMiterLimit="10" StrokeThickness="1" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeDashOffset="0" StrokeDashCap="Flat" Stretch="Fill" Width="20" Height="20" Opacity="1" RenderTransformOrigin="0,0" Cursor="Default" IsHitTestVisible="True" Visibility="Visible" x:Name="el" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="0">
      <Ellipse.Fill>
        <SolidColorBrush Color="#FF808080" Opacity="1" />
      </Ellipse.Fill>
    </Ellipse>
    <UserControl1 Width="0" Height="0" Opacity="1" RenderTransformOrigin="0,0" Cursor="Default" IsHitTestVisible="True" Visibility="Visible" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="0" />
    <Canvas Width="0" Height="0" Opacity="1" RenderTransformOrigin="0,0" Cursor="Default" IsHitTestVisible="True" Visibility="Visible" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="0">
     
        <Rectangle RadiusX="0" RadiusY="0" StrokeMiterLimit="10" StrokeThickness="1" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeDashOffset="0" StrokeDashCap="Flat" Stretch="Fill" Width="100" Height="100" Opacity="1" RenderTransformOrigin="0,0" Cursor="Default" IsHitTestVisible="True" Visibility="Visible" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="0">
          <Rectangle.Fill>
            <SolidColorBrush Color="#FFFFFFFF" Opacity="1" />
          </Rectangle.Fill>
          <Rectangle.RenderTransform>
            <TransformGroup>
              <RotateTransform CenterX="0" CenterY="0" Angle="25" />
            </TransformGroup>
          </Rectangle.RenderTransform>
        </Rectangle>
     
    </Canvas>
 
  <Canvas.RenderTransform>
    <TransformGroup>
      <RotateTransform CenterX="0" CenterY="0" Angle="80" />
    </TransformGroup>
  </Canvas.RenderTransform>
  <Canvas.Triggers>
    <EventTrigger RoutedEvent="Canvas.Loaded">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation From="0" To="10" AutoReverse="False" BeginTime="00:00:00" Duration="00:00:00" SpeedRatio="1" FillBehavior="HoldEnd" RepeatBehavior="00:00:00" Storyboard.TargetName="el" Storyboard.TargetProperty="Opacity" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Canvas.Triggers>
  <Canvas.Resources>
    <Storyboard AutoReverse="False" BeginTime="00:00:00" Duration="00:00:00" SpeedRatio="1" FillBehavior="HoldEnd" RepeatBehavior="00:00:00" x:Name="testSB">
     
        <DoubleAnimationUsingKeyFrames AutoReverse="False" BeginTime="00:00:00" Duration="00:00:00" SpeedRatio="1" FillBehavior="HoldEnd" RepeatBehavior="00:00:00">
          <DiscreteDoubleKeyFrame Value="5" KeyTime="00:00:00.5000000" />
        </DoubleAnimationUsingKeyFrames>
     
    </Storyboard>
  </Canvas.Resources>
</Canvas>

Limitations :

  1. custom prefixes is not suported. for example : <uc:UserControl1 ... will be resolved to <UserControl1
  2. due to silverlight's limitation for dependency properties, the ones that are not in the control's hirarechy except Canvas.Left, Canvas.Top, Canvas.ZIndex, Storyboard.TargetName and Storyboard.TargetProperty, are not supported.

Project :

please use the following link to download the project :

http://weblogs.asp.net/blogs/mehrantoosi/Silverlight/XAMLTools.zip

Thanks

-M

Attachment: XAMLTools.zip
Published Monday, March 03, 2008 7:17 AM by rambler_elf
Filed under: ,

Comments

# re: Silverlight's XamlWriter!

Monday, March 24, 2008 6:00 AM by Justin-Josef Angel [MVP]

Hi,

I've sent some traffic your way. Do you plan publishing a SL 2.0 version of the XamlWriter class?

# re: Silverlight's XamlWriter!

Tuesday, March 25, 2008 5:41 AM by rambler_elf

Hi Justin,

Yes I've planned to rewrite the class for SL 2.0.

do you wanna help me? :-)

Thanks

-Mehran

# re: Silverlight's XamlWriter!

Wednesday, April 09, 2008 12:39 PM by Dave Lowther

Mehran,

Great work here. I am looking foward to a SL2B1 release. In the meantime, I pulled the code into a SL2B1 class and it mostly worked. I had to hardcode a "return true" on line 17 of TypeValidator.cs and I changed line 166 of XamlWriter.cs to read:

if (val != null && val.GetType().ToString() == "System.Windows.DependencyObject")

Then I am able to at least get somewhere.

Thanks and I hope that helps you a bit!

Dave

# re: Silverlight's XamlWriter!

Thursday, April 10, 2008 9:35 AM by rambler_elf

Hi Dave,

Thanks for your help. ;)

I'm experiencing busy days at office, but I'm looking forward to take my time for rewriting the class.

Thanks again.

-Mehran

# re: Silverlight's XamlWriter!

Tuesday, August 19, 2008 3:03 PM by Rez

So, how does one include namespaces?

# re: Silverlight's XamlWriter!

Tuesday, October 07, 2008 12:57 AM by Anan

Hi

Waiting badly for your SL 2.0 Beta 2 version of XAML writer....

# re: Silverlight's XamlWriter!

Wednesday, December 10, 2008 4:20 PM by Abhijeet P

Are you looking to start a codeplex project. If so I'd be interested. Contact me at

abhidotnetATgmailDOTCOM

PS - Replace AT with an @

Leave a Comment

(required) 
(required) 
(optional)
(required)