Custom Renderer for Picker automatically changes layout in app (without overrides)

Simen91Simen91 NOMember

I wanted to make a custom renderer for the Picker because it doesn't re-measure it's width according to the SelectedIndex and managed to do that with this (sneaky) renderer:

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using DigitalFleetTool.Droid.Renderers;
using System.ComponentModel;

[assembly: ExportRenderer(typeof(Picker), typeof(CustomPickerRenderer))]
namespace DigitalFleetTool.Droid.Renderers
{
    public class CustomPickerRenderer : PickerRenderer
    {

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {

            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName.Equals("SelectedIndex"))
            {
                var picker = (Picker)sender;


                IVisualElementRenderer view = Platform.GetRenderer(picker);
                view.ViewGroup.SetMinimumWidth(1);

            }
        }

    }
}

However, I noticed it changed the layout of the Spinner from the Xamarin default spinner to what seems to be the Android default spinner. I like the Xamarin one because it can display all options at once in my app which makes it easier to use, so I removed the override (so it basically contains no code) but it STILL changes the Spinner! The largest problem is that the spinner it changes to doesn't seem to be reachable through the Control parameter and it's partly transparent (unusable) in my emulators. It seems to work on my real device, but I don't trust it to work on all devices after seeing it on the emulator.
Image of the Xamarin spinner http://imgur.com/a/bQJtA
Image of the Android spinner http://imgur.com/a/Jqnly

Answers

  • OstgodaOstgoda USMember ✭✭
    edited February 2018

    Same problem here! Does anyone know a solution for this?

    Edit:
    Minutes after posting, I found the solution for this!
    Just add the following using so it displays the Xamarin default spinner:
    using PickerRenderer = Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer;

    I found it in a similiar thread:
    "Be sure that if you're using AppCompat that you define
    using PickerRenderer = Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer; else it would be
    using Xamarin.Forms.Platform.Android.PickerRenderer"

  • princepotemkinprincepotemkin Member ✭✭

    This Xamarin 'software' is complete twaddle

Sign In or Register to comment.