views:

84

answers:

3

Just sample of behavior:

namespace XAMLParserBug
{
   public class MyCustomClass
   {
      public int ID { get; set; }
      public string Name { get; set; }
   }
}

then use it in XAML:

<UserControl x:Class="XAMLParserBug.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" 
    xmlns:local="clr-namespace:XAMLParserBug"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <local:MyCustomClass ID="1" Name="My Name With Spaces" />
    </UserControl.Resources>
 <Grid x:Name="LayoutRoot">

and then get the error:

'My Name With Spaces' is not a valid value for Name

Is it a XAML parser bug or well-known behavior?


Added:

Anyway, WPF XAML parser correctly processed Name property in this case (VS 2008 SP1).

+1  A: 

Don't use Name as a property in XAML: it is reserved by x:Name.

Michael S. Scherotter
Where described this limitation? Why xml namespaces (x:) are ignored?
Andir
+3  A: 

According the MSDN documentation here :

Each object element with a Name or x:Name attribute defined in the markup generates an internal field with a CLR name that matches the XAML name.

Hence any content in a Name property must conform to the rules for field identifier names.

Personnally I would have expected XAML to only create fields for items with using the x:Name form and relax its rules for Name at least for non-UIElement types but it doesn't.

AnthonyWJones
A: 

The Naming Grammar for XAML are found here:

http://msdn.microsoft.com/en-us/library/ms742534.aspx

None of the characters mentioned is a "space" so therefore the following is not allowed:

My Name With Spaces

However this is allowed:

MyNameWithSpaces

One of your comments makes me wonder (sorry if I am wrong) that you may be confusing namespaces with Names with Spaces as these concepts are seperate, as a namespace would be like:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

With the reference using a namespace below:

x:Name="ObjectName"
RoguePlanetoid
No, i am not confusing about xml namespaces. In my example `Name`-attribute is my custom property of string type and with no limitations. But XAML parser considered it as `x:Name` attribute that has special meaning and grammar limitations.My thougths about it: That behavior is a bug in XAML-parser. This limitation has meaning only for `FrameworkElement`-based classes.
Andir
Hey sorry about that, must have mis-understood, but usually Name is reserved - so maybe it is that I think you may be on the right lines - I have had some parser errors from XAML that I were for non-specific reasons!
RoguePlanetoid