Bug 57031 - C# style events not unbinding immediately from ViewTreeObserver.GlobalLayout events
Summary: C# style events not unbinding immediately from ViewTreeObserver.GlobalLayout ...
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2017-06-01 02:51 UTC by Kevin Ford
Modified: 2017-06-26 20:03 UTC (History)
2 users (show)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 57031 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Kevin Ford 2017-06-01 02:51:42 UTC
When subscribing to an event to the ViewTreeObserver.GlobalLayout on a view and then in the event handler unsubscribing from that event, the C# style event handlers receive the event three times even though the event is immediately unsubscribed in the event handler.

EventHandler onGlobalLayout = null;
onGlobalLayout = (sender, args) =>
    view.ViewTreeObserver.GlobalLayout -= onGlobalLayout;
    System.Diagnostics.Debug.WriteLine("Global Layout Happened");

view.ViewTreeObserver.GlobalLayout += onGlobalLayout;

When using the native style events and handlers, the event is only received once as the event is unhooked successfully and immediately the first time the event handler is called.


public void OnGlobalLayout()
    if (_View != null)
        if (Build.VERSION.SdkInt < BuildVersionCodes.JellyBean)
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning restore CS0618 // Type or member is obsolete
        System.Diagnostics.Debug.WriteLine("Global Layout Happened");

A working example of this inconsistency in how the events work can be found here:


The application output window will show the debug message being output three times in the C# event handler activity and only once for the native event handler activity even though they should work the same.
Comment 1 Jon Douglas [MSFT] 2017-06-26 20:03:30 UTC
Marking bug as CONFIRMED as I can see the inconsistency of this between the C# styled events and java listeners with the reproduction from:


The C# events show three invocations:

[0:] Global Layout Happened
[0:] Global Layout Happened
[0:] Global Layout Happened

vs. the Java listeners showing one invocation:

[0:] Global Layout Happened

Version Information:

Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.14) Release
Microsoft .NET Framework
Version 4.7.02046

Installed Version: Enterprise

Xamarin (fec6f88)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK (9dbc4c5)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK (ad35de4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.