简体   繁体   中英

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

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.

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 from Stuff<'T> . Not ideal at all but it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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