2016-09-01 3 views
3

this C# code을 번역하려고합니다. 지금까지 내 시도 :F의 사용자 지정 라우팅 이벤트 #

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이 무시됨)가됩니다.

내가 잘못하고있는 아이디어가 있습니까?

+0

간단히''을 쓰려고 했습니까? –

+0

@Filippo 예. 작동하지 않습니다. FrameworkElement 또는 UIElement에서 상속되지 않은 이벤트는 지정해야합니다. Button.Click. – Funk

+2

'static member'를'static let '으로 바꾸어보세요. –

답변

3

F #에서 표준이 아닌 꽤 많은 일을하기 때문에 까다 롭습니다. 여기

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 # 이벤트 관리를 사용하여 필요로하는 표준 이벤트의 동작을 오버라이드 (override) 할 필요가 있습니다

하는 C# 코드의 번역이 될 것이다 . 또한 F #에서 public static readonly 필드를 사용할 수 없기 때문에이 이벤트를 속성으로 래핑합니다. 라우트 된 이벤트를 구현하는 "표준"방식이 아니더라도 WPF는 이와 같은 문제가있는 것으로 보입니다.

+0

현명한 해결 방법! 고마워. 고마워. – Funk

관련 문제