如何在 Silverlight 4 和 XAML 中将 F# 类型与泛型结合使用?
采用以下 F# 代码:
type Blah<'T>(objects : 'T array) as this = // whatever
当我尝试在 XAML 文档中使用该类型时,没有与泛型参数关联的类型,而且它很丑陋。我认为编译器也会抱怨:
<ns:Blah foo="bar"/>
所以,我尝试像这样为类型添加别名(在我的 Blah.fs 文件的底部):
type StuffBlah = Blah<Stuff>
然后,当我在 XAML 文档中以相同的方式使用它时,找不到该类型存在:
<ns:StuffBlah foo="bar"/>
这是为什么呢?有没有更干净、更优雅的方法来做到这一点?我仍在掌握 Silverlight、XAML 和 F# 的窍门,因此我们将不胜感激任何建议。谢谢。
Take the F# following code:
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. 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):
type StuffBlah = Blah<Stuff>
Then when I use it in the same way in my XAML document, the type is not found to exist:
<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. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StuffBlah
版本不起作用的原因是 F# 语法的特定部分仅为 F# 项目创建类型别名,而不是创建实际类型。由于该名称在 IL 级别作为实际类型不可见,因此通常无法由 Silverlight 或 XAML 访问。解决此问题的一种方法是创建
StuffBlah
作为派生自Stuff<'T>
的第一类类型。一点也不理想,但它会起作用。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. Since the name is not visible at the IL level as an actual type it is not accessible to Silverlight or XAML in general.One way to work around this is to create
StuffBlah
as a first class type which derives fromStuff<'T>
. Not ideal at all but it will work.