I'm reading Silverlight 2 Unleashed, published in October 2008 and it has examples in it with a root canvas tag:
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Red"
Width="500"
Height="300">
<Ellipse Width="400"...
However when I create a new Silverlight Application in VS2008, I get a UserControl root tag:
<UserControl x:Class="TestFirst.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White"...
Of course I can change this by replacing the root tag with Canvas but then I have to also change the code behind to inherit from Canvas instead of UserControl and have to take out the InitializeComponent method.
Taken out of context, using a Canvas as the root in a XAML document might seem weird. However, in the book, this is used when we demonstrate the very first XAML samples in an online tool named SilverlightPad. In that case, there is no Code-Behind, only XAML, thus you do not need to change anything in a class file (since there is none).
The point is, any XAML element can be used as the root of a XAML document. What Visual Studio does by creating a UserControl XAML root, and linking it to a UserControl class in Code-Behind is really a special case of a more general scheme. I agree that it is what the reader will be confronted most of the time, but I also believe in the value of showing that things can be different. In addition, it is also important to show that sometimes, there is no Code-Behind and that XAML is a language with multiple features even without the "help" of Code-Behind class.
Silverlight 2 Unleashed uses a progression starting from pretty much zero, and with a (rather steep) learning curve. If you continue to read on, you will see that we start using Visual Studio a little later in the book, and things will become more familiar. However, you will have seen that you can use other elements as the root, and I think this has an educational value.
If you have any other questions, feel free to post here or to email me.
Cheers, Laurent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With