Using CreateFromXaml() in WPFe? XamlStringBuilder could save you time!

In WPFe JavaScript code, it's handy to be able to build up a xaml string dynamically, then call CreateFromXaml() to create the elements on the fly. What can be a pain is coding up your string buildup in JavaScript.

I wrote a quick tool (depends on 2.0 CLR) that allows you to type or paste xaml XML into a top pane, then generate the equivalent xaml string buildup code in JavaScript. You can then click a "Copy to Clipboard" button, return to your WPFe project, and paste in the string buildup code. (See attached file)

 Hope this is helpful!



Comments (6)
  1. Gavin Gear of the Ink team has created a WinForms app that takes a XAML string and turns it into a javascript

  2. says:

    At the risk of looking like I’m nitpicking a quick tool, there are a couple issues:

    1. If any of your attribute values contain an apostrophe, even if the apostrophe is represented as an "apos" entity, the resulting Javascript string is invalid XML.

    2. Concatenating strings in Javascript has the same overhead as it does in .NET — every concatenate copies the entire string. The generated code would be faster and consume less memory if it pushed the strings onto a Javascript array, then used the join() method of the array to generate the final string.

  3. InfinitiesLoop says:

    Nice. How about support for going the other way around? That way you don’t have to save the actual xaml if you want to update it, you can always get back to the xaml from the javascript.

  4. For those of you that attended my talk in Salt Lake City – here’s the links I referred to in my talk…

  5. interactive says:

    Gavin, you should change the JavaScript output to this:

    var a = [];




    var xaml = a.join("");

    If used often this is a performance benefit while running JavaScript.


  6. hala_zzz says:

    its great work but i wonder if this java script code could be used to directly embed xaml in html document  or even convert it to <canvas> html5 elements?

Comments are closed.

Skip to main content