F# 中的自定义路由事件

2024-05-26

我正在尝试翻译这段 C# 代码 https://msdn.microsoft.com/en-us/library/ms752288.aspx。到目前为止我的尝试:

type MyButtonSimple() as self =
    inherit Button()

    static let TapEvent =
        EventManager.RegisterRoutedEvent
            ( "Tap", RoutingStrategy.Bubble, 
              typeof<RoutedEventHandler>, typeof<MyButtonSimple>)

    let tapEvent = new Event<RoutedEventHandler, RoutedEventArgs>()
    let raiseTapEvent() =
        let newEventArgs = new RoutedEventArgs(TapEvent)
        tapEvent.Trigger(self, newEventArgs)

    [<CLIEvent>]
    member x.Tap = tapEvent.Publish 

    // For demonstration purposes we raise the event when clicked
    override self.OnClick() =
        raiseTapEvent()

主窗口.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:funk="clr-namespace:funk;assembly=appWPF"
    Title="Routed" Height="300" Width="400">
    <StackPanel Background="LightGray">
        <funk:MyButtonSimple Content="Spin" Background="#808080" Foreground="LightGray">
            <funk:MyButtonSimple.RenderTransform>
                <TransformGroup>
                    <RotateTransform x:Name="rotate"/>
                </TransformGroup>
            </funk:MyButtonSimple.RenderTransform>
            <funk:MyButtonSimple.Triggers>
                <EventTrigger RoutedEvent="funk:MyButtonSimple.Tap">
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior="Forever">
                            <DoubleAnimation 
                                Storyboard.TargetName="rotate"
                                Storyboard.TargetProperty="Angle"
                                From="0" To="90" Duration="0:0:2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </funk:MyButtonSimple.Triggers>
        </funk:MyButtonSimple>
    </StackPanel>
</Window>

单击该按钮不会启动动画。

更改代码RoutedEvent="funk:MyButtonSimple.Tap" to RoutedEvent="GotFocus"给出了一个工作示例(Button.Click 被覆盖)。

有什么想法我做错了吗?


这很棘手,因为它做了很多 F# 中不标准的事情。

该 C# 代码的翻译如下:

type MyButtonSimple() as self =
    inherit Button()

    static let tapEvent = 
       EventManager.RegisterRoutedEvent
            ( "Tap", RoutingStrategy.Bubble, 
              typeof<RoutedEventHandler>, typeof<MyButtonSimple>)

    // Create a custom event so you can override AddHandler/RemoveHandler behavior
    let tapEvent = 
        { new IDelegateEvent<RoutedEventHandler> with
            member this.AddHandler del = self.AddHandler(MyButtonSimple.TapEvent, del)
            member this.RemoveHandler del = self.RemoveHandler(MyButtonSimple.TapEvent, del) }

    // Raise via routed eventing strategy
    let raiseTapEvent() =
        let newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent)
        self.RaiseEvent newEventArgs

    // This isn't exactly the same, but public static fields aren't allowed in F#, and
    // this works for WPF
    static member TapEvent with get() = tapEvent  

    [<CLIEvent>]
    member x.Tap = tapEvent

    // For demonstration purposes we raise the event when clicked
    override self.OnClick() =
        raiseTapEvent()

这里的主要“陷阱”是您需要覆盖标准事件行为,这需要实现IDelegateEvent您自己进行操作,而不是使用普通的 F# 事件管理。此外,您无法在 F# 中执行公共静态只读字段,因此这会将事件包装到属性中。 WPF 似乎对此很满意,尽管这不是实现路由事件的“标准”方式。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

F# 中的自定义路由事件 的相关文章

随机推荐