22 people like it.

WPF Command in F#

demonstrate how to use object expression to create a WPF/Silverlight command.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
open System.Windows.Input

let createCommand action canExecute=
            let event1 = Event<_, _>()
            {
                new ICommand with
                    member this.CanExecute(obj) = canExecute(obj)
                    member this.Execute(obj) = action(obj)
                    member this.add_CanExecuteChanged(handler) = event1.Publish.AddHandler(handler)
                    member this.remove_CanExecuteChanged(handler) = event1.Publish.AddHandler(handler)
            }

let myCommand = createCommand
                        (fun _ -> ())
                        (fun _ -> true)
namespace System
namespace System.Windows
val createCommand : action:'a -> canExecute:'b -> 'c

Full name: Script.createCommand
val action : 'a
val canExecute : 'a
val event1 : Event<System.Delegate,'a>
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
property Event.Publish: IEvent<System.Delegate,'a>
abstract member IDelegateEvent.AddHandler : handler:'Delegate -> unit
val myCommand : 'a

Full name: Script.myCommand
Raw view Test code New version

More information

Link:http://fssnip.net/aP
Posted:12 years ago
Author:Tao Liu
Tags: wpf , silverlight