XAML in Longhorn Day 1 – April 03, 2004.
Longhorn Version 4053.
Greetings everyone!
This was my first taste of XAML.
First off, XAML is:
“"XAML" is an Extensible Markup Language (XML)-based language that enables developers to specify a hierarchy of objects with a set of properties and logic.”
That definition comes from the Microsoft site http://longhorn.msdn.microsoft.com/.
If you want to read more about what XAML is, please go where it is already written. I will not waste your time with any more explanation of what XAML is; I will just begin to waste your time with how to do XAML.
Also, at this point in time, I am not an XAML guru. But as I am learning this technology I will be posting my notes so you can benefit from my successes and failures. You can email me to ask questions or tell me im wrong too. he3 at he3 dot org
My first working XAML file.
First I will show you how to do it in an example, and then I will explain in more detail.
Open Notepad on a Longhorn machine. I have not installed any special SDK or tool. This is a fresh install of Longhorn Version 4053. Paste the following code in notepad and save the file as a .XAML file.
<Canvas ID="root"
xmlns="http://schemas.microsoft.com/2005/xaml">
<Button ID="Button001">Button001 Text</Button>
</Canvas>
Double click the .XAML file and a new window with a button opens.
Neat huh?
Onward…
Time to splain. From what I have read so far the Canvas Tag can be either a DockPanel, FlowPanel, or a Canvas Tag. Also, the xmlns is used to tell the parser which classes(assemblies and namespaces) to use. Every tag in Avalon sort of represents a class if I recall correctly.
Ok, so it appears everything is working really neato so far. But now I want to add some event handling to this file. With XAML a person can put the code to handle the events in the same file or a separate code behind file.
In the next example I attempt to put the event handling code in the same file. Later on I will most likely only use separate files but for now I want to keep it basic. Again, I am just using notepad with a fresh install of Longhorn. I have not installed any SDK or special tools.
First, to have code in the file I have to declare the Definition namespace. This is added to the Canvas/Root tag like s
<Canvas ID="root"
xmlns="http://schemas.microsoft.com/2005/xaml"
xmlns:def="Definition">
Then I can add the event handler to the button tag like s
<Button ID="Button001" Click="OnClick">Button001 Text</Button>
Then I can write my event handling code like s
<def:Code>
<![CDATA[
void OnClick(object sender, ClickEventArgs args)
{
Button001.Content = "Button001 Clicked";
}
]]>
</def:Code>
Everything together will look like this:
<Canvas ID="root"
xmlns="http://schemas.microsoft.com/2005/xaml"
xmlns:def="Definition">
<Button ID="Button001" Click="OnClick">Button001 Text</Button>
<def:Code>
<![CDATA[
void OnClick(object sender, ClickEventArgs args)
{
Button001.Content = "Button001 Clicked";
}
]]>
</def:Code>
</Canvas>
I save this file and attempt to open it and I get the following error:
MSAvalon.Window.Serialization.XamlParseException: Cannot specify an Event Handler with a Tag. Line 4 Offset 25 at…
After fooling around with the file and doing some Google searches I am not sure how to write the event handling code and have it compile. I believe what is happening is that in Longhorn, straight XAML files (non-dynamic) will display without compilation but if you write code you have to compile. This sounds good, but why is Longhorn not compiling the file when I double click it then? I will have to do some research tomorrow night.
So far, I am still super excited. It looks like a person can write one application that can be used for Web and Windows forms. Also the ‘sexiness’ of web design can be included in Windows forms. Ill be writing more in the next day or so, hopefully it won’t take long to figure out how to get the code working.
P.S. – Visual Studio 2005 will NOT install on Longhorn Version 4053. This really, really, really sucks IMHO. Does anyone know if I can install Yukon on Longhorn as of now?