How to get the spinner picker view in MAUI

Dorian Olarescu 20 Reputation points
2024-01-19T18:42:06.67+00:00

I am trying to get the picker with the spinner view on Android in MAUI. Before, in Xamarin.Forms, I used to be able to have a custom renderer with the redererer derived from Xamarin.Forms.Platform.Android (not the default one which is from ...Android.AppCompat). This is the picker I'm talking about: XamarinAndroidPickerCustomPopup

Now in MAUI I cannot seem to be able to use that style of picker anymore. Any ideas? Thanks!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,897 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,726 Reputation points Microsoft Vendor
    2024-01-22T02:56:35.2+00:00

    Hello,

    I used to be able to have a custom renderer with the redererer derived from Xamarin.Forms.Platform.Android

    In MAUI, you need to use Handler instead of Renderer to achieve this feature.

    Please refer to the following documentations:

    Update:

    This is because in Android, scrolling and selective pickers are different controls.

    After investigating, you could use the following steps to implement a simple scrolling picker.

    Step 1. Instantiate the MainActivity. This is because context objects are required to create native controls in Android.

    public class MainActivity : MauiAppCompatActivity
    {
        public static MainActivity Instance { get; private set; }
    
        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Instance = this;
        }
    }
    

    Step 2. Create a custom Picker.

    public class MyPicker : Picker
    {
    }
    
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:YourControlNamespace"
    
        <local:MyPicker x:Name="picker"
    Title="Select a monkey">
            <local:MyPicker.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>Baboon</x:String>
                    <x:String>Capuchin Monkey</x:String>
                    <x:String>Blue Monkey</x:String>
                    <x:String>Squirrel Monkey</x:String>
                    <x:String>Golden Lion Tamarin</x:String>
                    <x:String>Howler Monkey</x:String>
                    <x:String>Japanese Macaque</x:String>
                </x:Array>
            </local:MyPicker.ItemsSource>
        </local:MyPicker>
    

    Step 3. Create a custom Handler that pops up NumberPicker when you click on the EditText.

    public partial class MyViewHandler
    {
        public static PropertyMapper<MyPicker, MyViewHandler> PropertyMapper = new PropertyMapper<MyPicker, MyViewHandler>(ViewHandler.ViewMapper)
        {
    
        };
    
        public MyViewHandler() : base(PropertyMapper)
        {
        }
    }
    public partial class MyViewHandler : ViewHandler<MyPicker, View>
    
    {
        public MyViewHandler(IPropertyMapper mapper, CommandMapper? commandMapper = null) : base(mapper, commandMapper)
        {
        }
    
        protected override View CreatePlatformView()
        {
            var edit = new EditText(MainActivity.Instance);
            edit.Focusable = false;
            edit.FocusableInTouchMode = false;
            edit.Click += (s, e) =>
            {
                var p = this.VirtualView as Picker;
                var items = p.ItemsSource as IList<string>;
    
                var n = new NumberPicker(MainActivity.Instance);
    
                n.MaxValue = items.Count - 1;
                n.MinValue = 0;
                n.SetDisplayedValues(items.ToArray<string>());
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.Instance);
                var dlg = alertDialog.SetTitle(p.Title)
                .SetView(n)
                .SetPositiveButton("OK", (s, e) =>
                {
    
                    edit.Text = p.ItemsSource[n.Value].ToString(); // Assign the selected value to EditText
                })
                .Create();
                dlg.Show();
            };
            return edit;
        }
    }
    

    Step 4. Register this handler.

                builder
                    .UseMauiApp<App>()
                    .UseMauiCommunityToolkit()
                    .ConfigureMauiHandlers(handlers =>
                    {
    #if ANDROID
                        handlers.AddHandler(typeof(MyPicker),typeof(MyViewHandler));
    #endif
                    })
    

    After that, you'll be able to see a simple spinner wheel Picker.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more