在 XAML 中创建嵌套类的实例
在XAML文件(WPF UserControl)中,有没有办法引用另一个类“A”中定义的内部类“B”?
public class A
{
public class B
{
}
}
类似于:
<local:A.B ... />
此语法不起作用,因为“B”被解释为类“A”中名为“B”的属性。
我尝试过更奇特的语法,例如“::”或“+”,但似乎都不起作用。
我目前正在使用 Silverlight 4 和 VS2010。
预先感谢您的帮助。
in a XAML file (a WPF UserControl), is there a way to reference an inner class "B" defined in another class "A" ?
public class A
{
public class B
{
}
}
Something like :
<local:A.B ... />
This syntax does not work because "B" is interpreted as a property named "B" in class "A".
I've tried more exotic syntaxes like "::" or "+" but none seems to work.
I'm currently using Silverlight 4 with VS2010.
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题已经很老了,我不知道它是否适用于 2010 年的 WPF 版本,但现在您可以通过使用嵌套类型的“真实”(内部)名称来使其工作:
如果您曾经看过反汇编代码,这就是嵌套类型的样子:
This question is pretty old, and I don't know if it would have worked with the version of WPF back in 2010, but now you can make it work by using the "real" (internal) name of the nested type:
If you've ever looked a disassembled code, that's how nested types look like:
我一直在寻找,因为这是否可能,我想知道。不幸的是,我在 msdn 上发现了这个:
因此,看来您无法使用点运算符引用嵌套类。至于通过 XAML 访问该内部类的其他方法,我在搜索中还没有找到任何运气。 :o( 但这是一个相当有趣的问题,所以我会继续搜索。也许我会找到一些运气!:o)
I was searching and searching, because if this is possible, I would like to know. Unfortunately, I found this on msdn:
So, it appears you can't reference a nested class with the dot operator. As for alternative ways of getting to that inner class through XAML, I haven't had any luck in my searches yet. :o( But this is a rather interesting issue, so I will continue searching. Maybe I'll find some luck! :o)
.
指的是属性;不知道为什么 XAML 也不能搜索嵌套类,但事实并非如此。嵌套类可以在字符串内(例如属性值)表示,使用
A+B
而不是AB
code>:作为元素名称(如问题所示),不允许使用
+
,因为结果将不再是有效的 XML;+
不是有效的名称字符:XAML 是 XML< /a>.
XML 规范 - NameChar。
所以元素名称不能直接描述嵌套类。
但请参阅下面的更新 - 解决此问题的替代语法。
更新
根据 @Artfunkel 对一个答案的评论,这应该是一种解决方案 [我尚未测试]:
来自: https://learn.microsoft.com/en-us/dotnet/framework/xaml-services/x-type-markup-extension
待定如何使用该语法指定属性名称。使用 x:TypeArguments 吗?
.
refers to a property; not sure why XAML couldn't also search for a nested class, but it doesn't.A nested class can be represented when inside a string (e.g. a property value), using
A+B
instead ofA.B
:As an element name (as shown in question),
+
is not allowed, as the result would no longer be valid XML;+
is not a valid name character:XAML is XML.
XML Spec - NameChar.
So the element name cannot directly describe a nested class.
BUT see UPDATE below - an alternative syntax that solves this.
UPDATE
Per @Artfunkel's comment on one answer, this should be a solution [I have not tested]:
From: https://learn.microsoft.com/en-us/dotnet/framework/xaml-services/x-type-markup-extension
TBD how to specify property name with that syntax. Use
x:TypeArguments
?