为什么我不能在同一个程序集中使用 UserControl 上的 Name 属性? [英] Why can't I use the Name attribute on UserControl in the same assembly?

查看:26
本文介绍了为什么我不能在同一个程序集中使用 UserControl 上的 Name 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建 WPF UserControl 并尝试使用它时,出现以下编译器错误:

When I created a WPF UserControl and tried to use it, I got the following compiler error:

因为 'UserControl1' 在同一个程序集中实现,您必须设置 x:Name 属性而不是 Name 属性.

Because 'UserControl1' is implemented in the same assembly, you must set the x:Name attribute rather than the Name attribute.

该消息清楚地说明了如何修复它,但它的原因是什么?为什么我不能在这里使用Name?

The message is clear on what to do to fix it, but what is its cause? Why can't I use Name here?

推荐答案

x:Name 只是一种更明确的表达方式,即此特定 XML 命名空间中的名称属性".WPF 不能在没有给出这个提示的情况下编译它,因为它在同一个程序集中,这只是他们编写解析器的方式的限制.

x:Name is simply a more explicit way of saying "The name attribute in this specific XML namespace". The fact that WPF can't compile it without being given this hint because it's in the same assembly is just a limitation of how they wrote the parser.

如果你问为什么会这样,我不确定,因为不是我写的.它可能与需要能够在构建 UserControl1 之前将 Name 属性(读取:Dependency Property)解析为具体的东西有关,换句话说,catch-22.

If you are asking why it is this way, I do not know for sure because I didn't write it. It probably has something to do with it needing to be able to resolve the Name attribute (read: Dependency Property) to something concrete BEFORE building your UserControl1, in other words, a catch-22.

这篇关于为什么我不能在同一个程序集中使用 UserControl 上的 Name 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆