Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Custom renderer for Picker

$
0
0

Hi,
I'm really new to Xamarin and Xamarin Forms as well, so maybe it's a stupid question:

I'm trying to implement my own renderer for Xamarin.Forms.Picker, I'm starting from Custom renderers example.

I want to render the picker as a spinner in android, and I'm able to do this with this code

    [assembly: ExportRenderer (typeof (MyEntry), typeof (MyEntryRenderer))]
    class MyEntryRenderer : PickerRenderer
        {
            Spinner   mySpinner;
            // Override the OnModelChanged method so we can tweak this renderer post-initial setup
            protected override void OnModelChanged (VisualElement oldModel, VisualElement newModel)
            {
                base.OnModelChanged (oldModel, newModel);

                // lets get a reference to the native control
                var nativeEditText = (TextView) Control;
                // do whatever you want to the EditText here!
                mySpinner = new Spinner (this.Context);
                nativeEditText.SetBackgroundColor(global::Android.Graphics.Color.Yellow);
                mySpinner.SetBackgroundResource (  CustomRenderer.Android.Resource.Drawable.spinner_border);
                this.SetNativeControl (mySpinner);
            }
    }

and it works pretty well.

This class is obviously written in the Android project, and in the shared Forms project I have the class MyEntry, which is used to add the View in the layout like this

Content = new StackLayout {
                Children = {
                    new Label {
                        Text = "Hello, Custom Renderer !",
                    }, 
                    new MyEntry {

                    }
                },
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };

now what I'd like to do is to add some methods to MyEntry class where make calls to some methods in MyEntryRenderer, is that possible?

thank you very much


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>