开发者

How to implement custom events in F# on the compact framework?

开发者 https://www.devze.com 2023-01-15 02:08 出处:网络
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.

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


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号