我正在使用 Xamarin Forms 选择器控件并需要设置文本颜色,但是没有这样的属性。我已经尝试制作一个自定义渲染器,它在 android 和 ios 中对我有用(我最终重绘了控件)。 wp8.1平台没有Draw事件,renderer中的控件本身好像也没有设置文字颜色的属性。我还尝试更改选择器绑定(bind)的控件,但未成功。
目前,我已经在 PCL 中创建了可绑定(bind)属性 TextColor,该属性正在运行。我的渲染器代码如下所示(我去掉了所有测试代码,只放了基本代码,因为我还没有发现任何有用的东西,放我的代码只是为了让每个人都了解上下文)。 另请注意属性 Picker.TextColorProperty 不存在,这是我想要做的...
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;
}
}
}
}
无论如何,我想知道是否有人可能对如何执行此操作有更多的了解,并且可以对我有所启发。
最佳答案
您提到的 Picker
中没有可用的 TextColor
属性。
即便如此,我们仍然可以实现为 WindowsPhone
更改 Picker
文本颜色。
我假设您是从 PickerRenderer
继承的,因为您的代码示例中缺少它,我添加了一些额外的东西,因此这对其他人更有帮助:-
在PCL
中定义接口(interface):-
public interface ICustomPicker2
{
Xamarin.Forms.Color MyBackgroundColor { get; set; }
Xamarin.Forms.Color MyTextColor { get; set; }
}
在 PCL
中扩展 Xamarin.Forms
Picker
:-
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); }
}
}
像这样在类库中创建您的 WindowsPhone
渲染器:-
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));
}
请注意,如果您只想设置一次文本颜色,以上就是您所需要的。
但是,如果您想在初始设置后更改颜色,那么您将需要监听属性更改并按如下所示进行操作:-
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
//
if (e.PropertyName == "MyTextColor")
{
SetPickerTextColor((this.Element as CustomPicker2).MyTextColor);
}
}
您还需要从类库中导出渲染器:-
[assembly: ExportRendererAttribute(typeof(CustomPicker2), typeof(CustomPicker2Renderer))]
关于c# - 更改 Xamarin Forms for Windows Phone 8.1 中 Picker 控件的默认文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35162207/