EventHandler实现F#

时间:2018-02-24 14:45:21

标签: c# .net f# xamarin.ios eventhandler

我正在C#中实施以下F#代码:

    using Softweb.Xamarin.Controls.iOS;

public class DemoViewController : UIViewController, ICardViewDataSource
{

    public CardView DemoCardView { get; set; }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        DemoCardView = new CardView();

        //Wire up events
        DemoCardView.DidSwipeLeft += OnSwipe;
        DemoCardView.DidSwipeRight += OnSwipe;
        DemoCardView.DidCancelSwipe += OnSwipeCancelled;
        DemoCardView.DidStartSwipingCardAtLocation += OnSwipeStarted;
        DemoCardView.SwipingCardAtLocation += OnSwiping;
        DemoCardView.DidEndSwipingCard += OnSwipeEnded;
    }

    void OnSwipe(object sender, SwipeEventArgs e)
    {
        Console.WriteLine("View swiped.\n");
    }

    void OnSwipeCancelled(object sender, SwipeEventArgs e)
    {
        Console.WriteLine("Swipe cancelled.\n");
    }

    void OnSwipeStarted(object sender, SwipingStartedEventArgs e)
    {
        Console.Write("Started swiping at location {0}\n", e.Location);
    }

    void OnSwiping(object sender, SwipingEventArgs e)
    {
        Console.Write("Swiping at location {0}\n", e.Location);
    }

    void OnSwipeEnded(object sender, SwipingEndedEventArgs e)
    {
        Console.Write("Ended swiping at location {0}\n", e.Location);
    }
}

我不确定如何复制

DemoCardView.DidSwipeLeft += OnSwipe;
F#中的

语法(+=F#的等效项是什么)。我知道这是事件订阅的责任,这让我觉得我应该在Add中使用F#方法,但是我不确定如何。此外,onSwipeonSwipeCancelled ...函数的签名应该是什么。 DidSwipeLeft和其他人预期的类型为'IEvent<EventHandler<SwipeEventArgs>,SwipeEventArgs>

完整的API名为XCardView,可在此处找到:

https://components.xamarin.com/gettingstarted/XCardView/true

1 个答案:

答案 0 :(得分:2)

let onSwipe = fun args -> printfn "View swiped"

DemoCardView.DidSwipeLeft.Add(onSwipe)

或者,简单地

DemoCardView.DidSwipeLeft.Add(fun args -> printfn "View swiped")