views:

60

answers:

2

I tried following the example given on MSDN, but my code does not compile on the compact framework. It does compile on the normal framework, though.

type StorageComponent(game) =
    inherit GameComponent(game)

let title_storage_acquired_event = new Control.DelegateEvent<StorageEventHandler>()

Error message:

The type 'DelegateEvent' is not defined

A: 

If you take a look at the code in the CTP

C:\Program Files (x86)\FSharp-2.0.0.0\source\fsharp\FSharp.Core\event.fs

it will offer some clues (I'm guessing NETCF does not have del.DynamicInvoke). It might also offer clues as to what to do instead; I'm not sure, but hopefully someone else will chime in with a full answer.

Brian
+2  A: 

Based on the hint from Brian, it looks that the types DelegateEvent<'Delegate> and Event<'Delegate, 'Args> are not supported on .NET Compact Framework. This would mean that you cannot declare an event that uses an explicitly specified delegate type.

However, you can still use the Event<'T> type which creates an event of type Handler<'T> (which is a generic delegate type representing methods with two parameters of types obj and 'T):

type StorageComponent(game) = 
  inherit GameComponent(game) 

  let titleStorageAcquiredEvent = 
    new Event<StorageEventArgs>() 

  [<CLIEvent>] // If you want to create C# compatible event
  member x.TitleStorageAcquired = 
    titleStorageAcquiredEvent.Publish()

Assuming that the declaration of StorageEventHandler looks like this:

delegate void StorageEventHandler(object sender, StorageEventArgs args);

The example above should create more or less an equivalent code (with the only difference that it uses generic Handler<_> delegate type instead of your own StorageEventHandler).

Tomas Petricek
That's what I ended up doing, thanks. One thing that is not so nice with F# events is that the type of handlers must be Microsoft.FSharp.Core.Control.Handler<T>, IIRC. Used from C# (or other .NET languages), it might have been easier to use System.EventHandler<T>, instead.
Joh

related questions