Custom renderer and parameters

AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

Hi.
I have a "Entry" Custom Renderer (called "MyEntry").
I would like to pass a parameter to MyEntry constructor. For example I would like to pass an integer:

MyEntry entry = new MyEntry(5);

How can I create the Renderer so it can accept parameters in input? Is it possible?

Tagged:

Best Answer

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭
    edited November 2015

    Sure just make the value you pass in the MyEntry constructor accessible by a property and from within your MyEntry renderer access it by e.PropertyName in the OnElementChanged event

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @GeraldVersluis an example?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Thanks

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @GeraldVersluis I have a problem with your code.
    I have (in PCL)

    public class MyEntryDecimal : Entry
    {
        public int MaxDecimali { get; private set ;}
    
        public MyEntryDecimal(int maxDecimali){
            MaxDecimali = maxDecimali;
        }
    }
    

    and in Android

    protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);
            if (e.OldElement == null) {
                var nativeEditText = (global::Android.Widget.EditText)Control;
                nativeEditText.SetSelectAllOnFocus (true);
                int maxDecimali = 2;
                if (e.NewElement != null)
                    maxDecimali = e.NewElement.MaxDecimali;
    
                nativeEditText.SetFilters (new Android.Text.IInputFilter[]{ new MinMaxDecimalInputFilter (0,100000, maxDecimali) });
    

    // if(ColoreBordo != null)
    // nativeEditText.colo
    }
    }

    but

    e.NewElement.MaxDecimali
    

    does not exists (compile error....)

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    What's the error?
    From a quick scan over your code; have a look at how I have my ElementChangedEventArgs defined and how you did.

    This line

    protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
    

    Should be

    protected override void OnElementChanged (ElementChangedEventArgs< MyEntryDecimal > e)
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    ok
    solved with this

                var newElement = e.NewElement as MyEntryDecimal;
    
                if (newElement != null)
                    maxDecimali = newElement.MaxDecimali;
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Yes @GeraldVersluis you are correct.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Ops, no. @GeraldVersluis with your suggestion I have another compile error.
    No problem, my workaround works fine.
    Thanks again

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭

    How I get around this on custom controls where things are being less than pleasant is to do something like this at the top of my renderer:

    public new MyEntryDecimal Element
    {
        get
        {
            return (MyEntryDecimal) base.Element;
        }
        set
        {
            base.Element = value;
        }
    }
    
  • milindAdavalemilindAdavale USMember

    Hi @GeraldVersluis. For the below suggestion by you, I am getting an error saying,"No suitable method found to override"
    How do I resolve this? Am I missing something?

    What's the error?
    From a quick scan over your code; have a look at how I have my ElementChangedEventArgs defined and how you did.

    This line

    protected override void OnElementChanged (ElementChangedEventArgs e)
    Should be

    protected override void OnElementChanged (ElementChangedEventArgs< MyEntryDecimal > e)

  • milindAdavalemilindAdavale USMember

    Well, got mine working with @AlessandroCaliaro suggestion as follows:-

    var newElement = e.NewElement as MyEntryDecimal;

            if (newElement != null)
                maxDecimali = newElement.MaxDecimali;
    

    But with the few examples from web, i have seen that they do not require this fix. They directly get the access of the new defined property for the renderer. Can anyone explain for that? This discussion was really helpful though.

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    @milindAdavale you're right! But my code (wasn't) wrong, it's just that the signatures have changed as Xamarin.Forms has evolved :) The rest of the code should work as expected.

Sign In or Register to comment.