Bug 32335 - Using WebView events generated from WebUIDelegate result in WebView from being unusable
Summary: Using WebView events generated from WebUIDelegate result in WebView from bein...
Status: CONFIRMED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) (show other bugs)
Version: Master
Hardware: PC Mac OS
: Normal normal
Target Milestone: master
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-07-22 18:05 UTC by Aaron Bockover [MSFT]
Modified: 2016-09-26 21:58 UTC (History)
3 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Aaron Bockover [MSFT] 2015-07-22 18:05:15 UTC
By subscribing to an event on WebKit.WebView (such as WebView.UIMouseDidMoveOverElement), the web view will become useless unless some other events (presumably) are also subscribed to and handled.

For example:

    webView.UIMouseDidMoveOverElement += (sender, e) => {
        // never reached, presumably as the rest of the web view is
        // rendered broken now by this event subscription
    };

The web view can now not be interacted with... can't focus, select, etc.

Using the delegate pattern however works as expected:

    class CustomWebUIDelegate : WebUIDelegate
    {
        public override void UIMouseDidMoveOverElement (WebView sender,
            NSDictionary elementInformation, NSEventModifierMask modifierFlags)
        {
            // reached as expected
        }
    }

    webView.UIDelegate = new CustomWebUIDelegate ();

I ran into this while working on the Xamarin Inspector.

It seems the general rule of thumb is to just not use C# events since the generated delegates to provide events are often broken.

I suspect the generated delegates need to be more dynamic and interact with the ObjC runtime directly...

    class WebView {
        event EventHandler _backing_UIMouseDidMoveOverElement;

        void UIMouseDidMoveOverElementHandler (...)
        {
            _backing_UIMouseDidMoveOverElement?.Invoke (...);
        }

        public event EventHandler UIMouseDidMoveOverElement {
            add {
                if (_backing_UIMouseDidMoveOverElement == null)
                    RegisterProtocolMethodWithObjCRuntime (
                        "UIMouseDidMoveOverElement",
                        UIMouseDidMoveOverElementHandler);
                _backing_UIMouseDidMoveOverElement += value;
            }

            remove {
                _backing_UIMouseDidMoveOverElement -= value;
                if (_backing_UIMouseDidMoveOverElement == null)
                    UnregisterProtocolMethodWithObjCRuntime (
                        "UIMouseDidMoveOverElement",
                        UIMouseDidMoveOverElementHandler);
            }
        }
    }

¯\_(ツ)_/¯
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-07-27 05:54:24 UTC
This is how our event support works: we add handlers for all delegate methods (old design decision we can't really change now).

However we might be able to fix this for WebView by doing the bindings manually and overriding respondsToSelector (like we did for ABPeoplePickerNavigationController).

Note You need to log in before you can comment on or make changes to this bug.