3

この C# コードを翻訳しようとしています。これまでの私の試み:

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

MainWindow.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"を次のように変更するとRoutedEvent="GotFocus"、実際の例が得られます (Button.Click はオーバーライドされます)。

私が間違っていることはありますか?

4

1 に答える 1