Sunday, May 30, 2010

XAML Presentation Questions

During my presentation at PhillyNJ.NET there were a couple questions I could not immediately answer.

x:Name

When you create a blank Silverlight project in Visual Studio you start with the following code:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">

</Grid>
</UserControl>

Someone questioned why the Name property on the Grid is in the ‘x’ namespace when other properties aren’t. The ‘x’ namespace is the base XAML namespace. The XAML standard specifies a name property that applies to all elements which is references by x:Name. Classes that are serialized into XAML can also have their own Name property, for example the Grid element has a Name property. In WPF and Silverlight the Name property on the objects is mapped directly to the x:Name XAML property, so you could use either x:Name or Name and everything would work fine. The use of x:Name just seems to be done by convention.

Label vs Textblock

One of the more common controls in Winforms is the label control. There is no label control in the base Silverlight installation, but there is one if the Silverlight Toolkit. This brings up the question, what is the difference between a label and the standard TextBlock control? It turns out TextBlock isn’t technically a control. It derives from FrameworkElement, whereas Label derives from ContentControl which gives label extra functionality that TextBlock doesn’t have. For example a Label can use a control template, and can contain content other then plain text.

Non-Rectangular Windows

In Winforms it was a little tricky to do non-rectangular windows, but it’s actually pretty easy in WPF. There is a good example here, http://devintelligence.com/2007/10/shaped-windows-in-wpf/. I have not seen any indication that you can do non-rectangular windows in Silverlight. This actually wouldn’t be to useful since you would still be constrained to the rectangular browser window.

XAML UI for WPF and Silverlight Presentation

Thanks to everyone who attended my presentation at the May meeting of PhillyNJ.NET. Here are the Visual Studio 2010 projects for that I used in the demo:

XAML Basics Demo

XAML Data binding Demo