F# 在F中将成员定义为事件处理程序#
这可能被问了好几次,但我就是找不到一个例子 我的目标是为事件定义一个事件处理程序,该处理程序应该是类的成员。换句话说,我不想使用函数,因为我需要访问实例变量和成员 我尝试过的最新版本:F# 在F中将成员定义为事件处理程序#,f#,F#,这可能被问了好几次,但我就是找不到一个例子 我的目标是为事件定义一个事件处理程序,该处理程序应该是类的成员。换句话说,我不想使用函数,因为我需要访问实例变量和成员 我尝试过的最新版本: namespace A type ValueList<'TValueItem when 'TValueItem :> IValueItem>() = inherit System.Collections.ObjectModel.ObservableCollection&l
namespace A
type ValueList<'TValueItem when 'TValueItem :> IValueItem>() =
inherit System.Collections.ObjectModel.ObservableCollection<'TValueItem>()
// This is causing error: The value or constructor 'ValueList_CollectionChanged' is not defined
let collectionChangedHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ValueList_CollectionChanged)
// Constructor code
do base.CollectionChanged.AddHandler(collectionChangedHandler)
// Handles collection changed events for data items
member this.ValueList_CollectionChanged(sender : obj, e : System.Collections.Specialized.NotifyCollectionChangedEventArgs) =
// The code I want to run goes here
...
名称空间A
键入ValueList IValueItem>()=
inherit System.Collections.ObjectModel.ObservableCollection看起来像是在寻找自标识符语法:
type ValueList<'TValueItem when 'TValueItem :> IValueItem>() as this =
要使此方法保持有效,还需要将ValueList\u CollectionChanged
方法设置为当前格式:
member this.ValueList_CollectionChanged (sender : obj) (e : System.Collections.Specialized.NotifyCollectionChangedEventArgs) =
作为使用curried参数的替代方法,您可以使用lambda转换处理程序实例化的参数,例如.NotifyCollectionChangedEventHandler(fun sender e->this.(…)
member this.ValueList_CollectionChanged (sender : obj) (e : System.Collections.Specialized.NotifyCollectionChangedEventArgs) =