Skip to content

Compile Xaml to C# (.cs)

June 14, 2007

I recently published XamlT @ http://XamlT.com which is a tool that has several useful functions for xaml users.

1) You can view information about the Types expressible in different Xaml Vocabularies (WPF, SL 1.0, SL 1.1, etc..)

2) You can also convert Xaml files into C#

A little background on Xaml & C#

[you can replace the words C# or .cs below with VB and it is all still true]

In WPF, we compile Xaml into a combination of C# (.g.cs) and a special binary form (.baml).  So at runtime, in most cases, there doesn’t need to be any Xml parsing happening – baml is an optimized, pre-tokenized version of the elements that were specified in the .xaml file.

Once a window/page is displayed in WPF, often people programmatically change the state of the tree or add new subtrees to the tree.  If people know how to do something in Xaml, it isn’t always simple to know the code to do the same thing.

That is why I built the Xaml -> C# tool into XamlT.

Related posts from the past

From → XAML (non-UI)

11 Comments
  1. Rob permalink

    Alan Cobb said:

    Hi Rob,Nice tool.  I really like the ability to go from XAML to C#.My gripes:The choice of colors (bright yellow and bright red on complete black) is quite "non-standard" and bugging the heck out of me.  Any way that can be made setable?  There are other things about the UI that I\’d like to tweak/improve.Is it possible you could give us the source?  That\’s one of the things I love about Snoop and Petzold\’s XamlCruncher, both give you the source.  If you can\’t give us all the source, then could you hide the deep secrets in a central module that you don\’t release and give us the source to the UI?  For example, release a UI-less assembly that did the XAML to C# transformation, so we can put our own UIs on top of it?  This type of hardcore tool-app cries out for being open-sourced.Thanks,Alan Cobb (http://alancobb.com)
    But later wanted me to delete his email address from the comment…so I post the comment here without his email address.

  2. Unknown permalink

    More change requests:
    For the XAML to C# system:
    It would be nice if the split between the XAML and C# "panes" were a GridSplitter, in the same spirit as XamlPad or XamlCruncher.  I\’m always trying to make one of the two panes bigger than its current fixed size.
    Instead of the FlowDocumentPageViewer for the C# pane I would prefer a simple scrollable TextBox.  I realize it\’s supposed to be a "feature", but the problem is that the FlowDocumentPageViewer has its own rigid ideas about where the start and end of the pages should be.  With a simple scrolling TextBox I have more freedom to look at precisely the lines I\’m interested in.
    Re: "Open Source": If source was available you wouldn\’t need to find time to do these kinds of changes yourself.
    Again Rob, you\’ve created a very useful tool.  That\’s why I\’m using it and finding things to complain about🙂.
    Thanks,Alan Cobb

  3. Brian permalink

    Thanks for sharing this great tool–I\’m surprised that it is seemingly the only one like it available. 
     
    However–does this tool not support data templates and dependency properties? It seems to give "object reference not set to…" for all but the more basic scenarios.

  4. Rob permalink

    Glad you enjoy.
    I\’ll add DataTemplates to the list of ToDos.
    What is your DP scenario?  (show me the Xaml)!
    Thx, Rob 

  5. Brian permalink

    Here is a portion of my xaml below–I guess a suitable benchmark might be that whatever compiles in Expression blend should check out in the conversion tool. That was my original expectation but it is a valuable tool in any case.
     
    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="UntitledProject10.Window1" x:Name="Window" Title="Window1" Width="640&quot; Height="480">
     <Grid x:Name="LayoutRoot">  <ListBox> <ListBox.Resources>
      <DataTemplate x:Key="editableLanguage">   <StackPanel Margin="24,4,0,3" Orientation="Horizontal">    <TextBlock Height="18" VerticalAlignment="Center" FontWeight="Bold" Text="Binding Path=Text"  />    <TextBlock VerticalAlignment="Center" Text=" "  />    <ComboBox Margin="0" Padding="4,0,4,0"       Name="cmbFluencies"      Height="14" VerticalAlignment="Top" >    </ComboBox>    <Separator Width="15" Height="0"></Separator>   </StackPanel>  </DataTemplate>
      <DataTemplate x:Key="nonEditableLanguage">   <StackPanel Margin="24,4,0,3" Orientation="Horizontal">    <TextBlock VerticalAlignment="Center" Height="18" FontWeight="Bold" Text="Binding Path=Text"/>    <TextBlock VerticalAlignment="Center" Height="18" Text=" ("  />    <TextBlock VerticalAlignment="Center" Height="18" Text="Binding Path=Fluency"  />    <TextBlock VerticalAlignment="Center" Height="18" Text=")"  />   </StackPanel>  </DataTemplate>
      <Style TargetType="ListBoxItem" x:Key="lbiStyle">   <Setter Property="Margin" Value="0" />   <Setter Property="Padding" Value="0" />   <Setter Property="Template">    <Setter.Value>     <ControlTemplate TargetType="{x:Type ListBoxItem}">      <Grid>       <ContentPresenter Margin="0" Panel.ZIndex="1" />       <Canvas Margin="0" x:Name="bound" Panel.ZIndex="0"></Canvas>      </Grid>      <ControlTemplate.Triggers>       <MultiTrigger>        <MultiTrigger.Conditions>         <Condition Property="ListBoxItem.Name" Value="" />         <Condition Property="IsMouseOver" Value="true" />        </MultiTrigger.Conditions>        <Setter Property="ContentTemplate" Value="{StaticResource editableLanguage}" />        <Setter Property = "Background" Value="AliceBlue"   TargetName="bound"/>       </MultiTrigger>      </ControlTemplate.Triggers>     </ControlTemplate>    </Setter.Value>   </Setter>  </Style> </ListBox.Resources></ListBox>
     </Grid></Window>

  6. Unknown permalink

    lotro gold lord of the rings gold lord of the rings online gold lotro gold lord of the rings gold lord of the rings
    online gold Warhammer gold
    Warhammer online gold Warhammer money War gold War money Tabula Rasa Credit lotro gold lord of the rings gold lord of the rings online gold lord of the
    rings online gold lord of the rings
    gold Tabula Rasa Credit World of Warcraft gold PotBS Doubloon Pirates of the
    Burning Sea Doubloon PotBS Gold Pirates of the Burning Sea
    Gold lotro gold lord of the rings gold lord of the rings online gold Tabula Rasa Credit Warhammer gold Warhammer online gold PotBS Doubloon PotBS gold Pirates of the Burning Sea
    gold Pirates of the
    Burning Sea Doubloon

  7. Unknown permalink

    1fs2d4 0mexx1 wow gold wow gold wow gold wow gold mygamestock wow gold wow gold wow gold mygamestock 股票软件 股票软件 FX FX 黑马股 黑马股 胶体磨 胶体磨 上海水泵 上海水泵 真空泵 真空泵 东方医院 东方医院 wow power leveling wow power leveling wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold 黑马股 胶体磨 上海水泵 真空泵 东方医院

  8. Unknown permalink

    Welcome to enter (wow gold) and (wow power leveling) trading site, (wow gold) are cheap, (wow power leveling) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions! -144599524376527

  9. Unknown permalink

    Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of mp4 advertisement players, SD card players and advertisement LCD displays. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    Southern Stars Enterprises Co (Hong Kong Office)
    Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
    Tel:+852 2681 4099
    Fax:+852 2681 4586
    Southern Stars Enterprises Co (Shenzhen Office)
    Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
    Tel:+86 755 2592 9100
    Fax:+86 755 2592 7171
    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[aidheg

  10. Unknown permalink

    パソコン自作 アフィリエイト ブログアフィリエイト 多重債務 RMT 株式投資 結婚式 演出 太陽光発電 転職 看護師 釣具 会社設立 バイアグラ データ復旧 フローリング データ復元 投資 横浜 賃貸 ショッピング枠 現金化 データ復旧 テレマーケティング

  11. This tool was released in 2010, and has full support for compiling XAML into C#. Its a plugin for Visual Studio 2010. http://xamlgenerator.codeplex.com/

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: