Bug 45860 - “Control” Property In Custom Renderers and Effects Is Null For Layouts
Summary: “Control” Property In Custom Renderers and Effects Is Null For Layouts
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Paul DiPietro [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-10-25 01:04 UTC by Hakan
Modified: 2017-08-04 13:33 UTC (History)
4 users (show)

Tags:
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:
Status:
RESOLVED ANSWERED

Description Hakan 2016-10-25 01:04:05 UTC
When I try to write a custom renderer for any of the layout classes, the "Control" property appears null. It is OK since the renderer already is the wrapper for the native control but it is a problem when creating an Effect. So I cannot do any customization with the layout classes using Effects since Control property is not assigned.
Comment 1 Paul DiPietro [MSFT] 2017-03-01 15:39:06 UTC
Can you upload a project example of what you're attempting to do? Are you using SetNativeControl() at all?
Comment 2 Hakan 2017-05-04 18:28:03 UTC
I am not using SetNativeControl. You can create a barebone custom renderer class for a ContentView and AbsoluteLayout to try debugging the Control property. It will always be null.

It is not a problem for custom renderers because we can access the corresponding native View since the renderer inherits from it but that is not the case for Effects.
Comment 3 Paul DiPietro [MSFT] 2017-05-04 19:14:35 UTC
You should use SetNativeControl. Similar to the docs (https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/view/#Creating_the_Custom_Renderer_on_Android), you can have a custom renderer inherit from ViewRenderer<AbsoluteLayout, Android.Views.View> like this:

	public class TestLayoutRenderer : ViewRenderer<AbsoluteLayout, AView>
	{
		protected override void OnElementChanged(ElementChangedEventArgs<AbsoluteLayout> e)
		{
			base.OnElementChanged(e);
			if (Control == null)
			{
				var view = new AView(this.Context);
				view.SetBackgroundColor(Color.MediumPurple.ToAndroid());
				SetNativeControl(view);
			}
		}
	}

I've just used this and the Control is not null after calling SetNativeControl.
Comment 4 Hakan 2017-05-05 01:59:02 UTC
I was familiar with that approach for complex renderers where the author is adding native controls into the renderer but I was thinking of a simple AbsoluteLayout renderer similar to a ButtonRenderer.

For example when I create a button renderer, Control property is already set, I don't have to do anything. I thought layouts would be the same. But apparently I am mistaken. Thanks for the clarification. 

I am using a ContentView render without setting any native controls. So now I wonder, how does that work? Should I be setting a native AView for that, too?

Is there a documentation or code sample that shows simple layout/contentview renderers for both Android and iOS? (Because I didn't know about AView etc. before, so such examples would be nice)