Skip to content

Good Progress on Xaml to Code Tool

March 22, 2007

[update 6/10/2007: I’ve posted a version of this Xaml to Code tool on http://XamlT.com.  Select sample.xaml from the right side of the home page of the application and choose open.]

I mentioned last week that I was building a Xaml to Code converter.

 

It can now convert this Xaml:

<Window x:Class="xamlU.Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
   Title="xamlU" Height="471" Width="665" WindowState="Maximized">
   <Grid>
    <Grid.RowDefinitions>
       <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
   <Button IsDefault="True">Ok</Button>
   <Button Grid.Row="1">Cancel</Button>
  </Grid>
</
Window>

To this C#:

            Window Window1 = new Window();
            Window1.Title = "xamlU";
            Window1.Height = ((Double)new LengthConverter().ConvertFromString("471"));
            Window1.Width = ((Double)new LengthConverter().ConvertFromString("665"));
            Window1.WindowState = WindowState.Maximized;

            Grid Grid1 = new Grid();
            Window1.Content = Grid1;

            RowDefinition RowDefinition1 = new RowDefinition();
            RowDefinition1.Height = ((GridLength)new GridLengthConverter().ConvertFromString("*"));
            Grid1.RowDefinitions.Add(RowDefinition1);

            RowDefinition RowDefinition2 = new RowDefinition();
            RowDefinition2.Height = ((GridLength)new GridLengthConverter().ConvertFromString("*"));
            Grid1.RowDefinitions.Add(RowDefinition2);

            Button Button1 = new Button();
            Button1.IsDefault = true;
            Grid1.Children.Add(Button1);

            Button1.Content = "Ok";

            Button Button2 = new Button();
            Grid.SetRow(Button2, 1);
            Grid1.Children.Add(Button2);

            Button2.Content = "Cancel";

Required Enhancements

  • Handle Markup Extensions
  • Consider how x:Class should be handled
  • Support VB
  • Definitely more to think of and list here…

 

Possible Enhancements

  • Share instances of TypeConverters, instead of creating a new one for each property set.
  • Avoid using TypeConverters at all…figure out the object created from the type converter and convert it to code.

From → XAML (non-UI)

2 Comments
  1. Unknown permalink

    Grate idea!
    My main challenge is converting Markup Extensions to C# code.  Something like this
    <TextBox Text   ="{Binding Source={StaticResource str} Path=Text }"/>
     

  2. Rob permalink

    Yep, mapping some markup extensions to code will be a challenge…
    You can start by creating the markupextension and calling ProvideValue on it from code…but that won\’t always work as some rely on a ServiceProvider passed into ProvideValue…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: