简体   繁体   English

如何在Silverlight 4和XAML中使用具有泛型的F#类型?

[英]How can I use an F# type with generics in Silverlight 4 and XAML?

Take the F# following code: 以下代码取F#:

type Blah<'T>(objects : 'T array) as this = // whatever

When I try to use that type in a XAML document, there is no type associated with the generic parameter, and it's ugly. 当我尝试在XAML文档中使用该类型时,没有与泛型参数关联的类型,并且它很难看。 I think the compiler complains, too: 我认为编译器也抱怨:

<ns:Blah foo="bar"/>

So, I try to alias the type like so (at the bottom of my Blah.fs file): 所以,我试着像这样的类型别名(在我的Blah.fs文件的底部):

type StuffBlah = Blah<Stuff>

Then when I use it in the same way in my XAML document, the type is not found to exist: 然后,当我在XAML文档中以相同的方式使用它时,找不到存在的类型:

<ns:StuffBlah foo="bar"/>

Why is that? 这是为什么? Is there a cleaner, more elegant way to do this? 这样做有更清洁,更优雅的方式吗? I'm still getting the hang of Silverlight, XAML, and F#, so any advice would be greatly appreciated. 我仍然掌握Silverlight,XAML和F#,所以任何建议都会非常感激。 Thanks. 谢谢。

The reason the StuffBlah version doesn't work is that particular piece of F# syntax creates a type alias only for the F# project vs. creating an actual type. StuffBlah版本不起作用的原因是F#语法的特定部分仅为F#项目创建类型别名而不是创建实际类型。 Since the name is not visible at the IL level as an actual type it is not accessible to Silverlight or XAML in general. 由于名称在IL级别上不可见为实际类型,因此Silverlight或XAML通常无法访问该名称。

One way to work around this is to create StuffBlah as a first class type which derives from Stuff<'T> . 解决这个问题的一种方法是将StuffBlah创建为源自Stuff<'T>的第一类类型。 Not ideal at all but it will work. 根本不理想,但它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM