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

16 Comments

  • Hi,

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

  • Hi Justin,

    Yes I've planned to rewrite the class for SL 2.0.
    do you wanna help me? :-)

    Thanks
    -Mehran

  • 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

  • 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

  • So, how does one include namespaces?

  • Hi

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

  • Are you looking to start a codeplex project. If so I'd be interested. Contact me at
    abhidotnetATgmailDOTCOM
    PS - Replace AT with an @

  • 对不起,我英文很差,请原谅!
    我这里总是出错This Type is not supported by Xaml Writer
    我现在用的是3.0中文版,RegexOptions接口没有Compiled了, 所以只有将getSilverlightCompatibleXaml中的RegexOptions改了,但是改成什么都是上边的那个错误,用不了。你能不能说下是什么原因?我的Email:
    xiehaifeng666@163.com

  • Hi,your xamlwriter is good.but it don't work with textbox,image.I hope you will improve it.
    Thanks for your xamlwriter.

  • What's up, just wanted to say, I loved this blog post. It was helpful. Keep on posting!

  • Unquestionably believe that which you stated.

    Your favorite justification seemed to be on the internet the easiest
    thing to be aware of. I say to you, I certainly get irked while people think about worries that they just do not know about.
    You managed to hit the nail upon the top and also defined out the whole thing without
    having side effect , people could take a signal.

    Will probably be back to get more. Thanks

  • Excellent blog you have got here.. It's hard to find quality writing like yours nowadays. I honestly appreciate people like you! Take care!!

  • Truly when someone doesn't know afterward its up to other people that they will assist, so here it occurs.

  • It's really a great and helpful piece of info. I am satisfied that you simply shared this helpful info with us. Please stay us up to date like this. Thanks for sharing.

  • Greetings! Quick question that's completely off topic. Do you know how to make your site mobile friendly? My web site looks weird when viewing from my iphone 4. I'm trying to find a template
    or plugin that might be able to correct this problem.
    If you have any recommendations, please share. Cheers!

  • I truly love your website.. Very nice colors & theme.
    Did you build this website yourself? Please reply
    back as I'm planning to create my very own site and would love to learn where you got this from or exactly what the theme is named. Appreciate it!

Comments have been disabled for this content.