Change the default text color of a Picker control in Xamarin formats for Windows Phone 8.1

I use the Xamarin Forms selection control and require the text color to be set, however there is no such property. I tried to make my own renderer, which worked for me in android and ios (I finished redrawing the control). On the wp8.1 platform, there is no Draw event, and the control itself in the renderer has no properties for setting the text color. I also tried to change the control with which the collector communicates unsuccessfully.

I have currently created a bindable TextColor property in PCL that works. The code for my rendering is shown below (I deleted all of my test code and placed only the base code, as I haven’t found anything useful yet and put my code in order to keep everyone in context). Also note that the Picker.TextColorProperty property does not exist and is what I would like to do ...

using Namespace.CustomControls; using Namespace.WinPhone.Renderers; using Xamarin.Forms; using Xamarin.Forms.Platform.WinPhone; [assembly: ExportRendererAttribute(typeof(BindablePicker), typeof(BindablePickerRenderer))] namespace Namspace.WinPhone.Renderers { public class BindablePickerRenderer : PickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) { base.OnElementChanged(e); var picker = e.NewElement; BindablePicker bp = (BindablePicker)this.Element; if (this.Control != null) { var pickerStyle = new Style(typeof(Picker)) { Setters = { new Setter {Property = Picker.BackgroundColorProperty, Value = bp.BackgroundColor}, new Setter {Property = Picker.TextColorProperty, Value = bp.TextColor} } }; picker.Style = pickerStyle; } } } } 

In any case, I wonder if anyone can learn more about how to do this and can shed light on me.

+5
source share
1 answer

Picker does not have the TextColor property that you mention.

Even so, we can still change the text color of Picker for WindowsPhone .

I assume you are inheriting from PickerRenderer since it was not in your sample code, and I added some additional things, so this is more useful for others: -

Define the interface in PCL : -

 public interface ICustomPicker2 { Xamarin.Forms.Color MyBackgroundColor { get; set; } Xamarin.Forms.Color MyTextColor { get; set; } } 

Xamarin.Forms Picker in PCL : -

 public class CustomPicker2 : Xamarin.Forms.Picker , ICustomPicker2 { public static readonly BindableProperty MyBackgroundColorProperty = BindableProperty.Create<CustomPicker2, Xamarin.Forms.Color>(p => p.MyBackgroundColor, default(Xamarin.Forms.Color)); public static readonly BindableProperty MyTextColorProperty = BindableProperty.Create<CustomPicker2, Xamarin.Forms.Color>(p => p.MyTextColor, default(Xamarin.Forms.Color)); public Xamarin.Forms.Color MyTextColor { get { return (Xamarin.Forms.Color)GetValue(MyTextColorProperty); } set { SetValue(MyTextColorProperty, value); } } public Xamarin.Forms.Color MyBackgroundColor { get { return (Xamarin.Forms.Color)GetValue(MyBackgroundColorProperty); } set { SetValue(MyBackgroundColorProperty, value); } } } 

Create a WindowsPhone renderer as in the class library: -

 public class CustomPicker2Renderer : PickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) { base.OnElementChanged(e); var picker = e.NewElement; CustomPicker2 bp = (CustomPicker2)this.Element; if (this.Control != null) { var pickerStyle = new Style(typeof(Picker)) { Setters = { new Setter {Property = Picker.BackgroundColorProperty, Value = bp.MyBackgroundColor}, } }; SetPickerTextColor(bp.MyTextColor); picker.Style = pickerStyle; } } private void SetPickerTextColor(Xamarin.Forms.Color pobjColor) { byte bytR = (byte)(pobjColor.R * 255); byte bytG = (byte)(pobjColor.G * 255); byte bytB = (byte)(pobjColor.B * 255); byte bytA = (byte)(pobjColor.A * 255); // ((System.Windows.Controls.Control)(((System.Windows.Controls.Panel)this.Control).Children[0])).Foreground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(bytA, bytR, bytG, bytB)); } 

Please note that this is all you need if you just want to set the text color once.

However, if you want to change the color after its initial set, you will need to listen to the change in the property and act on it, as in the following: -

  protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); // if (e.PropertyName == "MyTextColor") { SetPickerTextColor((this.Element as CustomPicker2).MyTextColor); } } 

You will also need to export the renderer from the class library: -

 [assembly: ExportRendererAttribute(typeof(CustomPicker2), typeof(CustomPicker2Renderer))] 
+7
source

Source: https://habr.com/ru/post/1242128/


All Articles