Created attachment 19924 [details]
Sample project to reproduce.
After update from Xamarin.Mac 2 to Xamarin.Mac 188.8.131.523 , WebView.ClearedWindowObject event is not fired anymore.
See attachment for sample code to reproduce this bug.
Here's the documentation:
Use this method to set custom properties on the window object before the page is actually loaded. Every time a frame loads or is reloaded all DOM properties are cleared from the window object so the new page has a fresh window object to use. If the page you are loading depends on specific window object properties to exist, they should be added at this point before any scripts are executed.
So I assume the event should indeed be called when the page is loaded.
Any update on this bug? Any chance that it would be fixed in next release ?
It's really major problem from my point of view.
So use of the C# events like this:
this.someWebView.ClearedWindowObject += (sender, e) =>
technically works, specially in the simple cases, but often causes problems. Under the hood we are generating a delegate class for you, and bubbling events.
We try to catch a few mis-uses where you override the "fake" delegate, but not all of them are easily catchable.
An example of rewriting it in the recommended style:
This should not have broken, which is why I'm keeping the bug open, but since the work around is also the recommended way of proceeding it is a bit lower priority.
Thank you, this work around working for us. However i'm surprised that using events is not recommended in this case (why there are those events then? ) . From the usage point of the IWebFrameLoadDelegate is completely not clear to use - it does not has any methods, and you have to use attributes to export methods, which is error prone. Can you at least point to some documentation where this is explained ?
So, Xamarin.Mac (and iOS) have strong backwards compatibility levels that we do our best not to break whenever possible, so throughout our bindings there are sometimes less than ideal places we can't fix until the next time we're force to break API.
Exposing delegate calls as events dates back to even before my time, and much of the time, specially for simple cases, it just works. However, some cases like this shows issues (which are bugs, which is why this bug is still open) and can sometimes be confusing when reasoning about lifetime issues. I personally avoid it whenever possible.
You can read about this here (for iOS, but idea is identical):
Often, the workflow I use is:
- I want a notification / event from an object.
- Type object.Del and look for auto complete. See if there is one or more delegates properties.
- Look through each one (via apple docs or just to definition) to find the one i want, if there are more then one
- Assign this to the delegate, then have my current class implement the interface required to make it compile
- Then type "override" in between methods and look at the override list to find the item i'm interested in.
- Once I verify that works, possibly refactor the delegate owner to a more appropriate location.