Zhanbo Sun [MS]

Realize .NET & Windows Vista Potential

Avalon Q&A 101-102 (on XAML)

Q101: In XAML file, what can be used as x:Key value within Resources section?

Answer: A key for a dictionary can be String, LiteralExtension, TypeExtension and StaticExtension. You will get this error message from parsing the following XAML:
<Grid xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">
    <Grid.Resources>
        <SolidColorBrush Color="LightBlue" x:Key="{x:Null}"/>
    </Grid.Resources>
</Grid> 

Q102: What if I want to treat {x:Null} as string literal, instead of MarkupExtension?

Answer: {} is used in XAML file as escape sequence for this purpose. Parser will take whatever follows {} as string literal (with leading and trailing spaces trimmed). Try the sample below in which {}{x:Null} is used. Note: {} must be the first and second character.

<Grid xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">
    <Grid.Resources>
        <SolidColorBrush Color="LightBlue" x:Key="{}{x:Null}"/>
    </Grid.Resources>
    <Button Content="Sample 102" Background="{StaticResource {}{x:Null}}"/>
</Grid>

(This posting is provided "AS IS" with no warranties, and confers no rights.)
PostTypeIcon
4,636 Views

Comments