How to change Picker background Color in Xamarin MAC Platform

喜你入骨 提交于 2019-12-05 21:12:23

This seems to be caused for the new Theme used by Mojave.

One way to overcome this issue is by setting a value that will be visible on both Light and Dark them, for me it worked the Green.

Adding this to your XAML should be sufficient

<Picker.TextColor>
    <OnPlatform x:TypeArguments="Color">
        <On Platform="macOS" Value="Green"/>
    </OnPlatform>
</Picker.TextColor>

Making the change only on your MacOs project leaving the others as they are.

<Picker HorizontalOptions="CenterAndExpand" 
        VerticalOptions="CenterAndExpand">
    <Picker.Items>
        <x:String>Dell</x:String>
        <x:String>HP</x:String>
        <x:String>Mac</x:String>
        <x:String>Asus</x:String>
        <x:String>Lenovo</x:String>
        <x:String>Acer</x:String>
        <x:String>Micrsoft</x:String>
    </Picker.Items>
    <Picker.TextColor>
        <OnPlatform x:TypeArguments="Color">
            <On Platform="macOS" Value="Green"/>
        </OnPlatform>
    </Picker.TextColor>
</Picker>

Note: The TextColor will only affect the selected item text color.

Hope this helps.-

To change BackGroundColor of all Picker items for iOS you need to use custom renderer.

Your Picker in shared project

<StackLayout>
    <Picker x:Name="picCities" ></Picker>
</StackLayout>

In your ContentPage cs file assign ItemsSource to Picker

public MainPage()
{
    InitializeComponent();
    picCities.ItemsSource = new List<string> { "Hyderabad", "Bhopal", "Indore", "Jabalpur", "Mumbai", "Ahmedabad" };
    picCities.SelectedIndex = 0;
}

Now in your iOS project add one .cs file name PickerCustomRenderer & add this code

[assembly: ExportRendererAttribute(typeof(Picker), typeof(PickerCustomRenderer))]
namespace picker.iOS
{
    public class PickerCustomRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            UITextField textField = Control;
            UIPickerView pickerView = textField.InputView as UIPickerView;
            pickerView.BackgroundColor = UIColor.Red;
        }
    }
}

Output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!