Mando
Mando

Reputation: 11712

Xamarin.Forms and accessing native control within custom renderers

How to get the reference to the specific native view, not UIView? This is my custom Label renderer:

   public class MyLabelRenderer : LabelRenderer
   {
       protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
       {
            //how to get UILabel reference created by base LabelRender?
            base.OnElementChanged(e);
       }
   }

I have used this Renderer Sample as an example.

Upvotes: 1

Views: 4673

Answers (3)

Christian Findlay
Christian Findlay

Reputation: 7692

You need to use the Control property of the renderer. It theoretically should be available in the OnElementChanged method. But, on some platforms like UWP, the custom renderer just literally is the native control.

Upvotes: 0

Stephane Delcroix
Stephane Delcroix

Reputation: 16232

The reference you used is outdated. Use this one http://forums.xamarin.com/discussion/comment/59303

the native view can be accessed through the Control property, provided that you called SetNativeControl():

    protected override void OnElementChanged (ElementChangedEventArgs<MyCustomView> e)
    {
        base.OnElementChanged (e);

        if (e.OldElement == null) {
            // perform initial setup
            SetNativeControl (new UIMyCustomView (RectangleF.Empty));
        }

        UpdateSomething ();
    }

Upvotes: 1

Jason
Jason

Reputation: 89102

try using e.OldElement or e.NewElement

Upvotes: 0

Related Questions