A Tour of XAML - Part III - Type converters

Fundamentally XAML is an object initialization language. It creates object and sets values to properties. XAML, however, is not limit to creating reference types. You can also create instances of any type that has an associated type converter. For example, XAML can create an instance of a brush with the following code,

  <Brush>Red</Brush>

which creates a red solid color brush by calling the type converter associated with Brush. This means that,

  <Button>
    <Button.Background>
      <Brush>Red</Brush>
    <Button.Background>
  </Button>

is equivilent to,

  <Button Background="Red" />

XAML can be used to create value types, such as an integer, through its type converter as in,

  <s:Int32>27</s:Int32>

or a string,

  <s:String>It is the time for all good men...</s:String>

by using string's type converter. Note that XAML always refers to the CLR name for the type, not a language specific name such as "int" in C# or "Integer" in Pascal. Both of these types refer to the System.Int32 class in the CLR and XAML uses the CLR name, Int32. Note also that neither the XAML nor the WPF (Avalon) namespaces refer to the System namespace in mscorlib.dll where these types live. This mean that somewhere in the document you need to declare the prefix. This might look something like:

  <?Mapping ClrNamespace="System" XmlNamespace="System" 
  Assembly="mscorlib"?>
  <Window
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:s="System">
      <Button>
        <s:String>It is the time for all good men...</s:String>
      </Button>
  </Window>

Of course, for string, it is easier to write it,

  <Window xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
    <Button>It is the time for all good men...</Button>
  </Window>

but the string for is useful when you want multiple strings in a collection, say, for example, as the content of a list box,

  <?Mapping ClrNamespace="System" XmlNamespace="System" 
  Assembly="mscorlib"?>
  <Window
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:s="System">
      <ListBox>
        <s:String>It is the time for all good men...</s:String>
        <s:String>to come to the aid of their country.</s:String>
      </ListBox>
  </Window>

Note that if this had been written,

  <Window xmlns="http://schemas.microsoft.com/winfx/avalon/2005">
    <ListBox>
      It is the time for all good men...
      to come to the aid of their country.
    </ListBox>
  </Window>

the list box would only contain one string, not two. This is because all continuous text content is considered one string and not two. If you load this in WPF you will also notice that the string get treated as "It is time for all good men... to come to the aid of their country" where the spaces and line breaks have been collapsed. I will discuss the rules for whitespace normalization (collapsing) in a subsequent post. For now, if you are familiar with HTML, what XAML does shouldn't come as any surprise.

10/27/2005 4:59 AM

Wouldn't the use of <s:Int32>27</s:Int32> result in problems of 64 Bit systems.

I read that you should never use Int16, Int32, Int63 but Short, Integer, Long (in VB) because the length of an Int depends on your system architecture and your language maps e.g. Integer automatically.

Shouldn't Avalon provide this functionality, too?

Jan-Cornelius Molnar | http://janmdotnet.blogspot.com | Jan dot MolnarAT NOSPAMgmx dot net

10/28/2005 2:40 AM

Using Int32 doesn't cause problems on 64 bit systems.

VB's Short, Integer and Long map directly onto Int16, Int32, and Int64, respectively. They do not change between 32 bit and 64 bit systems. IntPtr does change but it is not CLS and you should avoid using it in any public APIs.

Chuck Jazdzewski | www.removingalldoubt.com | chuckjAT NOSPAMmicrosoft dot com

02/19/2006 9:41 PM

Do you intend to cover the details of collapsing and whitespace normalisation?

Dominic Hopton | mailto:dominichoptonAT NOSPAMhotmail dot com | dominichoptonAT NOSPAMhotmail dot com

Add New

Name

Email

Homepage

Security Word

Type in the security Word

Content (HTML not allowed)