只是行为示例:
namespace XAMLParserBug
{
public class MyCustomClass
{
public int ID { get; set; }
public string Name { get; set; }
}
}
然后在 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">
然后得到错误:
'My Name With Spaces' is not a valid value for Name
这是 XAML 解析器错误还是众所周知的行为?
已添加:
无论如何,WPF XAML 解析器在本例中正确处理了 Name 属性 (VS 2008 SP1)。
最佳答案
根据MSDN文档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.
因此,Name 属性中的任何内容都必须符合字段标识符名称的规则。
就我个人而言,我希望 XAML 只为使用 x:Name
表单的项目创建字段,并放宽其对 Name
的规则,至少对于非 UIElement 类型是这样,但是事实并非如此。
关于Silverlight 自定义类和 Name 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2107528/