Bug 52709 - WebView.ClearedWindowObject event is not fired
Summary: WebView.ClearedWindowObject event is not fired
Status: CONFIRMED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) (show other bugs)
Version: 3.0.0 (C9)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-02-23 17:34 UTC by dinoel
Modified: 2017-08-07 17:17 UTC (History)
3 users (show)

See Also:
Tags: Cycle9I
Is this bug a regression?: Yes
Last known good build: XM 2.x


Attachments
Sample project to reproduce. (347.54 KB, application/zip)
2017-02-23 17:34 UTC, dinoel
Details

Description dinoel 2017-02-23 17:34:12 UTC
Created attachment 19924 [details]
Sample project to reproduce.

Hello,

After update from Xamarin.Mac 2 to Xamarin.Mac 3.0.0.393 , WebView.ClearedWindowObject event is not fired anymore.

See attachment for sample code to reproduce this bug.
Comment 1 Vincent Dondain [MSFT] 2017-02-27 02:59:40 UTC
Right,

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.

https://developer.apple.com/reference/webkit/webframeloaddelegate/1501445-webview?language=objc
---

So I assume the event should indeed be called when the page is loaded.

Environment:
https://gist.github.com/VincentDondain/7ec3af0d064e2f2e480021301583a92a
Comment 2 dinoel 2017-07-31 09:34:01 UTC
Hi,

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.
Comment 3 Chris Hamons 2017-07-31 20:50:32 UTC
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:

https://gist.github.com/chamons/cb0da51a4c649f9693cff01b7d7fed3f

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.
Comment 4 dinoel 2017-08-06 16:45:04 UTC
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 ? 

Thanks !
Comment 5 Chris Hamons 2017-08-07 17:17:14 UTC
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):

https://developer.xamarin.com/guides/ios/application_fundamentals/delegates,_protocols,_and_events/#Protocols
and 
https://developer.xamarin.com/guides/ios/application_fundamentals/delegates,_protocols,_and_events/#Delegates

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.

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