Creating your own XamlPad

25 Feb 2008

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 (notstatic.com) which is very good and it has good Xaml intellisense!

Anyway, lets get started with Buddi-XamlPad.

  1. My application is as shown below.

Capture

  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();
gd.ColumnDefinitions.Add(
new ColumnDefinition());
gd.ColumnDefinitions.Add(
new ColumnDefinition());

outputButton =
new Button();
outputButton.Content =
"Your XAML output!";
gd.Children.Add(outputButton);
Grid.SetColumn(outputButton,
0);
outputButton.Click += ButtonClick;

rtb =
new RichTextBox();
rtb.Name=
"xaml";
rtb.AcceptsReturn =
true;
gd.Children.Add(rtb);
Grid.SetColumn(rtb,
1);
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)
ProcessXaml();
}
private static void ButtonClick(object sender,RoutedEventArgs args)
{
ProcessXaml();
}
public static void ProcessXaml(){
try{
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();
xd.Load(reader);
xd.DocumentElement.SetAttribute(
"xmlns","http://schemas.microsoft.com/winfx/2006/xaml/presentation");
StringBuilder sb =
new StringBuilder();
StringWriter sw=
new StringWriter(sb);
xd.Save(sw);

//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!