I want to detect mouse hovering over visual elements in .NET MAUI ( like buttons ). I cannot find any "onmouseover"/"ismouseover" events for this. How can I implement this.
2 Answers
All requests for "desktop" style control of keyboard and mouse have been consolidated under Maui issue: Desktop platforms: Mouse and Keyboard.
jfversluis' comment (at that issue or a related issue) indicates that no work in this area has yet been planned.
That issue is the place to discuss the topic (express interest in, or add any specific requirements or use cases), until there is a plan.
Until then, the work-around is generally to write Windows-only code in the Windows
folder of your Maui project.
Unfortunately for mouse, that only gives window or desktop-relative mouse information. AFAIK, There currently is no easy way to interact with individual UI elements while the mouse moves. (Because Maui started as Xamarin started as mobile cross-platform code; touch devices don't have concept of cursor being moved around screen.)
A crude hack is to use AbsoluteLayout
: position UI elements at exact positions, so you know where they are, then write (Windows-only) code that does your own "detection".
The next level of sophistication would be to write code that traverses a window's "visual tree" of UI elements, to detect what control the mouse is over. Maui must have such logic internally somewhere, to handle touch events. Unless it relies on each platform's UI code to decide that. TBD.
For now, Maui makes sense for apps that can live with a "touch" paradigm, rather than a "desktop" paradigm.
This isn't an answer per say, but as a suggestion you could try and determine when the mouse would be over the VisualElement and then use StateTriggers to actively apply a VisualState.
-
1It's really great helping others. But same time, having vauge answers make it harder for those who are looking for help. In your case, question is asking a suitable way to trigger the event. But you are suggesting how to do something else after triggering the event. Please re read the question. Sep 12, 2022 at 10:41
-
1While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Sep 14, 2022 at 8:28
-
@GayanKavirathne - While I agree this answer would be improved if it had a code snippet in it, I disagree with your understanding of the question. The question says detect mouse hovering. And mentions
onmouseover
, which would be code executed when the mouse hovers. There is nothing in the question about triggering mouse hover. In fact, it is impossible to "trigger" mouse hover; the mouse hovers when it does, it is not something under programmer's control. Dec 14, 2022 at 0:29