Bug 43327 - Using OnParentSet to unwire events does not work
Summary: Using OnParentSet to unwire events does not work
Alias: None
Product: Forms
Classification: Xamarin
Component: Samples ()
Version: 2.3.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Paul DiPietro [MSFT]
Depends on:
Reported: 2016-08-13 01:50 UTC by John Hardman
Modified: 2017-06-19 18:50 UTC (History)
4 users (show)

Tags: OnParentSet
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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description John Hardman 2016-08-13 01:50:56 UTC
The Xamarin.University guest lecture "Adding Analytics to you Mobile App" (by Michael Stonis) shows code overriding OnParentSet to wire and unwire event handlers on Views. The following is one example:

		protected override void OnParentSet ()
			base.OnParentSet ();

			if (Parent != null) {
				this.Clicked -= TrackingButton_Clicked;
				this.Clicked += TrackingButton_Clicked;
				this.Clicked -= TrackingButton_Clicked;

Using Xamarin.Forms 2.3, I never see the else part of this code be executed. It seems that OnParentSet is not called with Parent = null. It's unclear whether this constitutes a defect in Xamarin.Forms, a defect in the guest lecture code, or just the lecture code being out of date compared to the latest Xamarin.Forms version. One of those would seem to be true.

If OnParentSet is not intended for unwiring event handlers, what is the recommended way of cleaning up event handlers in custom controls?
Comment 1 Oleg Demchenko 2016-10-11 17:32:09 UTC
@parmendra please confirm
Comment 2 Parmendra Kumar 2016-10-12 17:30:50 UTC

I have checked this issue and observed that OnParentSet is called successfully with Parent = null.

Screencast: http://www.screencast.com/t/aizH1nXZur

SampleProject: https://www.dropbox.com/s/vmbnlssc3ifvv07/App68%20%281%29.zip?dl=0

Please use lattest Xamarin.Forms and Sample project and let me If I have missed anything else. 

Comment 3 Samantha Houts [MSFT] 2017-06-19 18:50:56 UTC
Because we have not received a reply to our request for more information we are closing this issue. If you are still encountering this issue, please reopen the ticket with the requested information. Thanks!