Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the Silverlight root tag be UserControl or Canvas?

Tags:

silverlight

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.

  • Is using the Canvas tag as the root standard or is the book going about an alternate way to create Silverlight applications?
  • What are the advantages of using Canvas instead of UserControl as the root element?
  • Why would the book for its first examples create silverlight applications in a different way than is given by default in Visual Studio?
  • Is there a way to have a canvas tag generated as the root tag by default in Visual Studio?
like image 379
Edward Tanguay Avatar asked Oct 15 '22 17:10

Edward Tanguay


1 Answers

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

like image 104
LBugnion Avatar answered Oct 18 '22 05:10

LBugnion