在 XAML 中创建嵌套类的实例

发布于 2024-10-04 02:00:49 字数 403 浏览 3 评论 0原文

XAML文件(WPF UserControl)中,有没有办法引用另一个类“A”中定义的内部类“B”?

public class A
{
    public class B
    {
    }
}

类似于:

<local:A.B ... />

此语法不起作用,因为“B”被解释为类“A”中名为“B”的属性。

我尝试过更奇特的语法,例如“::”或“+”,但似乎都不起作用。

我目前正在使用 Silverlight 4VS2010

预先感谢您的帮助。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等数载,海棠开 2024-10-11 02:00:49

这个问题已经很老了,我不知道它是否适用于 2010 年的 WPF 版本,但现在您可以通过使用嵌套类型的“真实”(内部)名称来使其工作:

<local:A+B />

如果您曾经看过反汇编代码,这就是嵌套类型的样子:

ParentTypeName+Nested

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:

<local:A+B />

If you've ever looked a disassembled code, that's how nested types look like:

ParentTypeName+Nested
若水微香 2024-10-11 02:00:49

我一直在寻找,因为这是否可能,我想知道。不幸的是,我在 msdn 上发现了这个:

您的自定义类不能是嵌套的
班级。嵌套类和“点”
其一般 CLR 使用语法会干扰其他 WPF 和/或 XAML
附加属性等功能。

因此,看来您无法使用点运算符引用嵌套类。至于通过 XAML 访问该内部类的其他方法,我在搜索中还没有找到任何运气。 :o( 但这是一个相当有趣的问题,所以我会继续搜索。也许我会找到一些运气!:o)

I was searching and searching, because if this is possible, I would like to know. Unfortunately, I found this on msdn:

Your custom class must not be a nested
class. Nested classes and the "dot"
in their general CLR usage syntax interfere with other WPF and/or XAML
features such as attached properties.

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)

别念他 2024-10-11 02:00:49

. 指的是属性;不知道为什么 XAML 也不能搜索嵌套类,但事实并非如此。


嵌套类可以在字符串内(例如属性值)表示,使用A+B而不是AB code>:

<Label SomeProperty1="{x:Static local:A+B.SomeProperty2}" />

作为元素名称(如问题所示),不允许使用 +,因为结果将不再是有效的 XML; + 不是有效的名称字符:
XAML 是 XML< /a>.
XML 规范 - NameChar

所以元素名称不能直接描述嵌套类。
但请参阅下面的更新 - 解决此问题的替代语法。


更新
根据 @Artfunkel 对一个答案的评论,这应该是一种解决方案 [我尚未测试]:

<x:Type TypeName="local:A+B"/>

来自: 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 of A.B:

<Label SomeProperty1="{x:Static local:A+B.SomeProperty2}" />

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]:

<x:Type TypeName="local:A+B"/>

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文