1

I have some code like this that works fine:

type App() =
    inherit Application()

    let stack = StackLayout(VerticalOptions = LayoutOptions.Center)
    let label = Label(XAlign = TextAlignment.Center, Text = "Welcome to F# Xamarin.Forms!")

    do
        let tapRecognizer = new TapGestureRecognizer()
        let handleTapEvent (sender:Object) (args:EventArgs) =
            label.Text <- "Tapped at " + DateTime.Now.ToString() 
            ()

        let tapEventHandler = new EventHandler(handleTapEvent)
        tapRecognizer.Tapped.AddHandler(tapEventHandler)
        label.GestureRecognizers.Add(tapRecognizer)

However when I change the args from EventArgs to a derived type like this:

type TapEventArgs(someId:int) = 
          inherit EventArgs()
          member this.SomeId = someId

        let handleTapEvent (sender:Object) (args:TapEventArgs) =
            label.Text <- args.SomeId.ToString() + " tapped"
            ()

I get the following error when I call AddHandler

The type 'EventArgs' is not compatible with the type 'TapEventArgs'

Also, If I change the EventHandler like this:

let tapEventHandler = new EventHandler<TapEventArgs>(handleTapEvent)

I get this error

 This expression was expected to have type 'EventHandler' 
but here has type    'EventHandler<TapEventArgs>'

Any way to force that derived type?

2 Answers 2

2

F# does not insert downcasts automatically the way C# does (and this is a good thing, not a bug). You cannot pass a descendant type where an ancestor type is expected.

In order to call AddHandler you need to insert a downcast manually using the downcast operator :>, like this:

tapRecognizer.Tapped.AddHandler(tapEventHandler :> EventHandler<EventArgs>)

When the target type is already known (like in your case), you can use an underscore in its place to let the F# compiler infer it from the context:

tapRecognizer.Tapped.AddHandler(tapEventHandler :> _)
2
  • Adding the downcast in the AddHandler method does not work. tapRecognizer.Tapped.AddHandler(tapEventHandler :> EventHandler<EventArgs>) throws This expression was expected to have type 'EventHandler<EventArgs>' but here has type 'EventHandler' Nov 17, 2018 at 12:02
  • What's the type of tapRecognizer.Tapped? Nov 18, 2018 at 17:06
1

The problem here is that the types are different, and that is what F# is telling you. The type of tapRecognizer.Tapped is of EventHandler, so when it is invoked it will be of type EventHandler ie not EventHandler<TapEventArgs> which is a different type. There is no way to change this, and subclassing TapGestureRecognizer is also not possible as the class is sealed.

Additionally, the code you posted would be difficult to compile, as it has circular references which F# prevents. TapEventArgs requires label which is defined in App. App depends on TapEventArgs. It would not be possible to update the UI from TapEventArgs instead a function would need to be passed in, or the state exposed.

There is a solution using the Command pattern, which allows some information to be passed to the callback and avoids circular dependencies. TapGestureRecognizer has a property CommandParameter where a value (of type obj) can be set. A callback can be supplied via the Command property that can receive that value. Here is the full example:

open Xamarin.Forms
open System

type App() as this =
    inherit Application()

    let stack = StackLayout(VerticalOptions = LayoutOptions.Center)
    let label = Label(XAlign = TextAlignment.Center, Text = "Welcome to F# Xamarin.Forms!")

    do
        let tapRecognizer = new TapGestureRecognizer()
        let handleTapEvent (x:obj) = 
            match x with 
            | :? int as someId -> label.Text <- someId.ToString() + " tapped"
            | _ -> label.Text <- "Tapped at " + DateTime.Now.ToString() 

        tapRecognizer.Command <- new Command(Action<obj>(fun x -> handleTapEvent x))
        label.GestureRecognizers.Add(tapRecognizer)
        stack.Children.Add label

        tapRecognizer.CommandParameter <- 42 // The value to be passed to the Command's callback
        this.MainPage <- ContentPage(Content = stack)

Note that casting must be used, because of the way the types are defined in Xamarin.Forms (using obj for the property).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.