4

After updating the Xamarin Library and MAC OS to latest system, facing issue in Picker Background color and color of item that is selected/focused in picker for MAC Platform in my visual studio 2017.

Picker that is filled with multiple values

Here is the picture of Picker that is filled with multiple value

On opening the picker can not able to set the background color and the item that is selected is also not visible because of its color

On opening the picker can not able to set the background color and the item that is selected is also not visible because of its color.

How can I set the background color of that picker and the color of that focused/selected item of picker?

V-Vek P-Tel
  • 77
  • 1
  • 10

2 Answers2

3

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.-

pinedax
  • 9,246
  • 2
  • 23
  • 30
  • 1
    From v3.4 They have changed OnPlatform xaml now. It's `OnPlatform Android="foo" iOS="foo"` it just gives warning though – Morse Jan 06 '19 at 15:13
  • Yes this is the one and last option that i already think off if not have any solution. Thank you for guidence. – V-Vek P-Tel Jan 07 '19 at 06:02
0

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

enter image description here

R15
  • 13,982
  • 14
  • 97
  • 173
  • I want to change the background color of the Items that are in Picker When open the Picker the list get opened i want to change the color of that opened list In Second Picture when i click on picker it opens the list of element is is in light blue color i want to change the background color of that list – V-Vek P-Tel Dec 26 '18 at 13:55
  • According to your question heading you need to change background color. You can change picker bg color, picker text color(item text color). There is nothing like items color. – R15 Dec 26 '18 at 13:58
  • When open the Picker the list get opened i want to change the bakgroungcolor of that opened list In Second Picture when i click on picker it opens the list of element it is in light blue color i want to change the background color of that list – V-Vek P-Tel Dec 26 '18 at 14:00
  • @V-VekP-Tel - I have added screenshot in answer. Do you need picker like that? – R15 Dec 26 '18 at 15:50
  • Yes exactly i want to change the background color of the list for the picker but picker i developed is a user defined control and that is for MAC OS – V-Vek P-Tel Dec 27 '18 at 04:32
  • UITextField is component of UIKit that is supported in IOS. I am not able to use same for MAC Platform – V-Vek P-Tel Dec 28 '18 at 10:28
  • @V-VekP-Tel - Is this question about xamarin forms? – R15 Dec 31 '18 at 05:42
  • I have picker control in xamarin.forms and i made it custom renderer for MAC platform so for MAC i want to change the color. it was working fine before update of Xamarin library – V-Vek P-Tel Jan 02 '19 at 06:37
  • I want to change the color of the item that is selected – V-Vek P-Tel Jan 02 '19 at 08:47