Xamarin.Mac - Slider handlers for ValueChanged and MouseUp?

lcocea 96 Reputation points
2021-09-03T13:50:21.607+00:00

I am new to Xamarin.Mac coming from WPF and I can't seem to figure out how to separately handle two Slider events: ValueChanged and MouseUp (or equivalent). The target-action mechanism seems to allow for declaring just one handler/action, Activated. In WPF this is straightforward, there are two event handlers available. I am only interested in a solution for Xamarin.Mac, not for Xamarin.Forms.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,312 questions
0 comments No comments
{count} votes

Accepted answer
  1. lcocea 96 Reputation points
    2021-09-06T14:12:27.7+00:00

    I think I figured it out. An Action is a "handler" that is called when the ViewController is notified that an event has gone down in a child control. It is triggered by at least one of the events that are raised when the user interacts with the control (e.g., for a button, it's a click; for a Slider, it's a value change). The developer has no access to the actual default event handlers on this level.

    To access/override the actual event handlers, one has to subclass (? overclass) the control*. This is best done in Xcode (just type in the new class name at the top of the control's Identity Inspector tab) as it will generate the two .cs files required in VS for Mac. Then one can override the handlers for events such as MouseUp in the newly created class. This actually works.

    Here comes the confusing part.

    • Consider a subclassed ImageButton. If an Action is defined for this new class where Mouse events are overridden, the Action somehow interferes with the new mouse event handlers. For example, if the new MouseUp handler doesn't call base.MouseDown then the Action is not triggered; on the other hand, if it does call base.MouseDown then the Action is triggered but other mouse event handlers, such MouseUp or MouseDragged, are apparently no longer raised, as if those events have already been handled by the Action. Therefore, one should not define an Action and only use the overridden events instead; but then one also needs to notify the parent when the events are raised. This can be done by registering a new handler in the child, which is called be the overridden event(s), and also register an event listener in the parent with its own handler. To summarize: child control raises custom event, which calls handler, then parent is notified and calls its own handler that can, for example. read child's properties and do stuff. This actually works but it shouldn't be so complicated.
    • There is no obvious way for the developer to choose the event that triggers the Action.
    • *By using an Outlet, one can also append (+=) a Clicked or Activated (?) event handler, but not a MouseUp handler (VS says it's a 'group method'). However, the mouse handlers of the main window can be overridden without creating a subclass.
    0 comments No comments

0 additional answers

Sort by: Most helpful