Adding key values for items in picker

前端 未结 3 1966
借酒劲吻你
借酒劲吻你 2021-02-07 12:34

I am using a XAMARIN picker to select a country. The countries are hard coded in the picker. Is there a way I could identify each country name through a key value. I have done t

3条回答
  •  被撕碎了的回忆
    2021-02-07 12:48

    No, Key property is available in xamarin picker. But, you can implement it using Dictionary class and SelectedIndex property of xamarin picker class.

    Implement it by using following code snippet :

    class PickerDemoPage : ContentPage
            {
                // Dictionary to get Color from color name.
                Dictionary nameToColor = new Dictionary
                {
                    { "Aqua", Color.Aqua }, { "Black", Color.Black },
                    { "Blue", Color.Blue }, { "Fuschia", Color.Fuschia },
                    { "Gray", Color.Gray }, { "Green", Color.Green },
                    { "Lime", Color.Lime }, { "Maroon", Color.Maroon },
                    { "Navy", Color.Navy }, { "Olive", Color.Olive },
                    { "Purple", Color.Purple }, { "Red", Color.Red },
                    { "Silver", Color.Silver }, { "Teal", Color.Teal },
                    { "White", Color.White }, { "Yellow", Color.Yellow }
                };
    
                public PickerDemoPage()
                {
                    Label header = new Label
                    {
                        Text = "Picker",
                        FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                        HorizontalOptions = LayoutOptions.Center
                    };
    
                    Picker picker = new Picker
                    {
                        Title = "Color",
                        VerticalOptions = LayoutOptions.CenterAndExpand
                    };
    
                    foreach (string colorName in nameToColor.Keys)
                    {
                        picker.Items.Add(colorName);
                    }
    
                    // Create BoxView for displaying picked Color
                    BoxView boxView = new BoxView
                    {
                        WidthRequest = 150,
                        HeightRequest = 150,
                        HorizontalOptions = LayoutOptions.Center,
                        VerticalOptions = LayoutOptions.CenterAndExpand
                    };
    
                    picker.SelectedIndexChanged += (sender, args) =>
                        {
                            if (picker.SelectedIndex == -1)
                            {
                                boxView.Color = Color.Default;
                            }
                            else
                            {
                                string colorName = picker.Items[picker.SelectedIndex];
                                boxView.Color = nameToColor[colorName];
                            }
                        };
    
                    // Accomodate iPhone status bar.
                    this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
    
                    // Build the page.
                    this.Content = new StackLayout
                    {
                        Children =
                        {
                            header,
                            picker,
                            boxView
                        }
                    };
    
                }
            }
    

    Source : https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/

提交回复
热议问题