Bug 31442 - Reuse renderer for a subclass of the native control
Summary: Reuse renderer for a subclass of the native control
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 1.4.2
Hardware: All All
: Normal enhancement
Target Milestone: ---
Assignee: Jason Smith [MSFT]
URL:
: 35609 ()
Depends on:
Blocks:
 
Reported: 2015-06-28 05:33 UTC by Andrei.N
Modified: 2016-04-27 20:41 UTC (History)
3 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 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 Andrei.N 2015-06-28 05:33:01 UTC
Currently it's not possible to reuse an existing renderer for a subclass of the native control.
 For example, have the EntryRenderer implementation create a MyUITextField instead of a UITextField, where 

public class MyUITextField : UITextField 
{ 
    public override CGRect TextRect(CGRect forBounds)
    // some customizations
    } 
}

Currently, the EntryRenderer is creating the UITextField like this (disassembler code):

public class EntryRenderer : ViewRenderer<Entry, UITextField>
{
 protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
 {
            base.OnElementChanged(e);
            UITextField control = base.Control;
            if (base.Control == null)
            {
                <b>UITextField uITextField = new UITextField(RectangleF.Empty);</b>
                control = uITextField;
                base.SetNativeControl(uITextField);
                this.defaultTextColor = control.TextColor;
                control.BorderStyle = UITextBorderStyle.RoundedRect;
                MessagingCenter.Subscribe<IVisualElementRenderer>(this, "Xamarin.ResignFirstResponder", (IVisualElementRenderer sender) => {
                    if (base.Control.IsFirstResponder)
                    {
                        base.Control.ResignFirstResponder();
                    }
                }, null);
                control.EditingChanged += new EventHandler(this.OnEditingChanged);
                control.ShouldReturn = new UITextFieldCondition(this.OnShouldReturn);
                control.EditingDidBegin += new EventHandler(this.OnEditingBegan);
                control.EditingDidEnd += new EventHandler(this.OnEditingEnded);
            }
            if (e.NewElement != null)
            {
                this.UpdatePlaceholder();
                this.UpdatePassword();
                this.UpdateText();
                this.UpdateColor();
                this.UpdateKeyboard();
            }
        }
}
One solution would be to add a abstract method in  ViewRenderer<TView, TNativeView>:

public abstract class ViewRenderer<TView, TNativeView> : VisualElementRenderer<TView>
where TView : View
where TNativeView : UIView
{
    protected abstract TNativeView CreateNativeControl();
}

public class EntryRenderer : ViewRenderer<Entry, UITextField>
{
    protected override UITextField CreateNativeControl()
    {
         return new UITextField();
    }
}

public class MyEntryRenderer : EntryRenderer
{
    protected override MyUITextField CreateNativeControl()
    {
         return new MyUITextField();
    }
}
Comment 1 outring 2015-08-06 07:52:24 UTC
The same thing here. Also it would be great to have one more virtual method for native control initialization, to avoid `Control == null` heuristics in `OnElementChanged`. I.e.:

public class EntryRenderer : ViewRenderer<Entry, UITextField>
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        ...
        if (Control == null)
        {
            var control = CreateNativeControl();
            InitializeNativeControl(control);
            SetNativeControl(control);
        }
        ...
    }

    ...

    protected virtual void InitializeNativeControl(UITextField control)
    {
        ...
        control.BorderStyle = UITextBorderStyle.RoundedRect;
        ...
    }

    protected virtual UITextField CreateNativeControl()
    {
         return new UITextField();
    }
}
Comment 2 Paul DiPietro [MSFT] 2015-11-23 22:41:12 UTC
*** Bug 35609 has been marked as a duplicate of this bug. ***
Comment 3 Jason Smith [MSFT] 2016-04-10 02:55:41 UTC
Thank you for taking the time to submit this feature request. We like this idea and have added it to our internal feature tracking system.

Warm regards,
Xamarin Forms Team
Comment 4 Andrei.N 2016-04-27 20:41:48 UTC
So this is resolved just by the fact that it was added in other tracking system?