Bug 27145 - Button.Clicked won't fire if SetNativeControl is used in iOS custom renderer
Summary: Button.Clicked won't fire if SetNativeControl is used in iOS custom renderer
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.4
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-02-17 10:23 UTC by tim.ahrentlov
Modified: 2015-10-12 09:24 UTC (History)
6 users (show)

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

Test case to reproduce bug (4.11 MB, application/zip)
2015-02-17 10:23 UTC, tim.ahrentlov

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 tim.ahrentlov 2015-02-17 10:23:19 UTC
Created attachment 9879 [details]
Test case to reproduce bug

To reproduce:

1. Load attached project.
2. Run on iOS.
3. Click button (red text)
4. Notice the message.
5. Go to CustomButtonRenderer and remove // from SetNativeControl line
6. Run again.
7. Click button (red text)
8. Notice that the message does not show.

I get the error on 1.3.1, 1.3.3 and 1.3.4 pre 4
Comment 1 Adam Kemp 2015-02-17 11:27:41 UTC
Looking at the ButtonRenderer class, this line should be outside the "if (base.Control == null)" block:

base.Control.TouchUpInside += new EventHandler(this.OnButtonTouchUpInside);

Alternatively, you could provide a protected method to fire the pressed event.
Comment 2 tim.ahrentlov 2015-02-17 11:32:59 UTC

I might misunderstand something, but shouldn't your proposed code (or something akin til this suggestion) happen automatically when calling base.OnElementChanged (e); AFTER the new control has been created and assigned using SetNativeControl?
Comment 3 Ram Chandra 2015-02-17 11:40:41 UTC
I have checked this issue and I am able to reproduce this issue.

I have followed steps mentioned in bug description and observed that when following line is uncommented, I am not getting any message.

SetNativeControl ( UIButton.FromType(UIButtonType.Custom) );

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

IDE logs: https://gist.github.com/saurabh360/94935cd1e43031a3d61f

Additional Info: This is working fine on android. I am only getting this error for iOS only.

Environment Info:


=== Xamarin Studio ===

Version 5.7.1 (build 17)
Installation UUID: 6ea47b0d-1852-4aaf-808d-373ff0a5002b
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: d508c8e
Build date: 2015-02-15 22:17:46-0500

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/jatin66/Desktop/Backup/android-sdk-macosx
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 507010017
Git revision: 0bc7d3550b6b088ac25b08dcf7bbe73bcc8658b3
Build date: 2015-02-03 19:43:29-05
Xamarin addins: f7b7d34419c9ec24501bfa7c658e80a6305613e0

=== Operating System ===

Mac OS X 10.10.0
Darwin Jatin66s-iMac.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 4 Adam Kemp 2015-02-17 11:59:36 UTC
The normal way to override which native control is used in a renderer subclass is to do this:

    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        if (Control == null)
            SetNativeControl(new MyCustomControl);

The code in ButtonRenderer looks like this:

		protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
			if (e.NewElement != null)
				if (base.Control == null)
					base.SetNativeControl(new UIButton(UIButtonType.RoundedRect));
					this.buttonTextColorDefaultNormal = base.Control.TitleColor(UIControlState.Normal);
					this.buttonTextColorDefaultHighlighted = base.Control.TitleColor(UIControlState.Highlighted);
					this.buttonTextColorDefaultDisabled = base.Control.TitleColor(UIControlState.Disabled);
					base.Control.TouchUpInside += new EventHandler(this.OnButtonTouchUpInside);

The only way to get the event attached with that code is to not call SetNativeControl yourself. If you call it before calling base.OnElementChanged then the event is never attached since the control is already not null. If you do it after then the event is attached to the wrong button (the one you're not using).

The fix I suggested was to attach the event outside of the if block. Now that I think about it, that's wrong because it might attach the event multiple times. I think the correct fix is to provide the protected method to fire the event yourself using your own handler.
Comment 5 tim.ahrentlov 2015-02-17 13:58:48 UTC
I'm rather hoping that they will put the "Init" code into a separate method that is either called by both SetNativeControl and the init-parts of the super.OnElementChanged, or can be called manually.
Comment 6 Jason Smith [MSFT] 2015-03-02 09:35:07 UTC
Basically yes what Tim says is essentially our long term plan. A separate method you can override to provide the base type.

For now you cannot simply replace the Control as indicated in this bug report. You can implment a button renderer in full without much difficulty however. Casting the button to IButtonController should give you some clues on what to do.
Comment 7 Adam Kemp 2015-06-03 11:54:17 UTC
Jason, IButtonRenderer is internal! Once again you have made doing simple customization of a built-in control impossible without reflection. Please stop making fundamental aspects of the renderer/element relationship internal!
Comment 8 aspyct 2015-10-12 09:24:06 UTC
What's the actual status on this ticket? It's marked as resolved, but I still encounter the issue with the latest (to date) version of Xamarin Forms.