FrameworkElement.Name 问题
我试图在构造函数中设置页面的 Name 属性:
public partial class PageListView : Page
{
public PageListView(string title)
{
InitializeComponent();
Name = title;
}
}
但是,我经常收到以下错误消息。
'x' is not a valid value for property 'Name'.
其中 x
似乎几乎是任何东西,深入研究异常详细信息似乎没有提供任何有用的信息(例如,InnerException 为空。)
有人知道这里发生了什么吗?
I am attempting to set the Name property of a Page in the constructor:
public partial class PageListView : Page
{
public PageListView(string title)
{
InitializeComponent();
Name = title;
}
}
However, I often get the following error message.
'x' is not a valid value for property 'Name'.
Where x
seems to be almost anything, drilling down into the exception details doesn't seem to provide any useful information (e.g. the InnerException is null.)
Does anyone know what is happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Name 属性通常遵循 C#/VB.NET 标识符(即字段)的规则。基于文档:
根据您传递的参数(即标题),您似乎可能违反了这一点。但你必须给出一些具体的例子才能确定。
The Name property generally follows the rules of C#/VB.NET identifiers (i.e. fields). Based on the documentation:
Based on the parameter you are passing (i.e. title), it seems like you may violate that. But you'd have to give some specific examples to be sure.
当然,在发布此消息后不久,我就意识到发生了什么事。
由于 FrameworkElement.Name 用于创建对象引用,因此您必须确保该字符串仅包含对象实例变量名称的有效字符。
请改用 Title 或其他纯文本属性,除非您确实想要设置
x:Name
属性以供引用。Of course, moments after posting this I realised what's going on.
Because FrameworkElement.Name is used for creating object references, you have to ensure that the string contains only valid chars for an object instance variable name.
Use Title or another plain text property instead, unless you really want to set the
x:Name
property for referencing.