开发者

Silverlight custom class & Name attribute

开发者 https://www.devze.com 2022-12-17 10:16 出处:网络
Just sample of behavior: namespace XAMLParserBug { public class MyCustomClass { public int ID { get; set; }

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 p开发者_如何学编程arser bug or well-known behavior?


Added:

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


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.


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


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"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号