Sunday, February 24, 2008

Creating your own XamlPad

When I was learning WPF Development through the Petzold’s book, in Chapter 19 the author talks a lot about XamlReader (and less about Xaml as such ;), Guess Petzold likes to code more than what he likes to design). Anyway during this I implemented my own XamlPad called “Buddi-XamlPad” which has two meanings

  1. Buddi is my nickname
  2. Buddi in Telugu mean “small”.

So the xamlpad is like a small-xamlpad like application. It does nothing but render your xaml and it is really really stupid. If you are looking for a better xamlpad than what that ships with VS SDK, then you better take a look at Kaxaml ( which is very good and it has good Xaml intellisense!

Anyway, lets get started with Buddi-XamlPad.

  1. My application is as shown below.


  1. You have a class called BuddiXamlPad which has a static method GetXamlWindow(). It returns an instance of Window which you can run in your application by saying “new Application().Run(BuddiXamlPad.GetXamlWindow())”
  2. The code for the BuddiXamlPad class is shown below.
class BuddiXamlPad
static Window wi;
static RichTextBox rtb;
static Button outputButton;
public static Window GetXamlWindow()
wi =
new Window();
Grid gd =
new Grid();
new ColumnDefinition());
new ColumnDefinition());

outputButton =
new Button();
outputButton.Content =
"Your XAML output!";
outputButton.Click += ButtonClick;

rtb =
new RichTextBox();
rtb.AcceptsReturn =
wi.KeyDown += WinKeyDown;
wi.Content = gd;

wi.Title =
"Buddi rocks with this XamlPad!";
return wi;

private static void WinKeyDown(object sender, KeyEventArgs args)
if(args.Key == Key.F5)
private static void ButtonClick(object sender,RoutedEventArgs args)
public static void ProcessXaml(){
TextRange tR =
new TextRange(rtb.Document.ContentStart,rtb.Document.ContentEnd);
string xaml = tR.Text;
StringReader reader =
new StringReader(xaml);

//Append Xaml Namespace
XmlDocument xd = new XmlDocument();
StringBuilder sb =
new StringBuilder();
StringWriter sw=
new StringWriter(sb);

//Now Read the complete XAML.
XmlReader xmlReader = new XmlTextReader(new StringReader(sb.ToString()));
object obj = XamlReader.Load(xmlReader); //Get the objects of the corrs. Xaml
outputButton.Content = obj;
catch(Exception ex){ MessageBox.Show(ex.Message);}

The code is pretty simple, the only problem I initially faced was that I had to add “xmlns” to the WPF namespace to the root of the xaml I write into the RichTextbox, which is kind of lame if not fixed. So I used Xml API to read the Xaml as XML and added the namespace. Isn’t it fun? Not many comments in the code but hey its all like 40-50 lines and I don’t think anyone would be bothered to look at it. Wish, I had time so that I would have done something good but given my masters’ thesis and other projects I do, I hardly have any time left.

Well the point is that it is pretty much possible to write good applications with .NET Framework and with every release it is getting bigger and better. Microsoft Rocks!

No comments: