简体   繁体   中英

EventHandler Implementation F#

I am implementing the following C# code in 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);
    }
}

I am not sure however how to replicate the

DemoCardView.DidSwipeLeft += OnSwipe;

syntax in F# (what is the equivilant of += in F# ). I know this is responsible for event subscription, which makes me think I should use the Add method in F# for this, however I'm not sure how. Also, what are the signatures of the onSwipe , onSwipeCancelled ... functions should be. The type expected by DidSwipeLeft and the others is 'IEvent<EventHandler<SwipeEventArgs>,SwipeEventArgs>

The full API is called XCardView and can be found here:

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

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

DemoCardView.DidSwipeLeft.Add(onSwipe)

Or, simply

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM