在Windows Phone 8.1的Xamarin Forms中更改Picker控件的默认文本颜色 [英] Changing the default text color of a Picker control in Xamarin Forms for Windows Phone 8.1

查看:85
本文介绍了在Windows Phone 8.1的Xamarin Forms中更改Picker控件的默认文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin Forms选择器控件,并且需要设置文本颜色,但是没有这种属性.我尝试制作一个自定义渲染器,该渲染器在android和ios中对我有用(我最终重新绘制了控件).在wp8.1平台中,没有Draw事件,并且渲染器中的控件本身似乎没有设置文本颜色的属性.我还尝试过更改选择器绑定到的控件失败.

I am using Xamarin Forms picker control and require setting the text color, however there is no such property. I have tried making a custom renderer which worked out for me in android and ios (I ended up redrawing the control). In the wp8.1 platform there is no Draw event and the control itself in the renderer doesn't seem to have the properties to set the text color. I have also attempted changing the control the picker binds to unsuccessfully.

当前,我在正在运行的PCL中创建了可绑定属性 TextColor .渲染器的代码如下所示(我剥离了所有测试代码,并且只放置了基本代码,因为我还没有发现任何有用的东西,并且只是为了让所有人都处于上下文状态而已). 还要注意,属性Picker.TextColorProperty不存在,这是我想做的...

Currently I have created the bindable property TextColor in the PCL which is working. The code for my renderer is shown below (I have stripped all my test code and am putting only the basic code since I havent found anything useful yet and am putting my code just to keep everyone in context). Also note that the property Picker.TextColorProperty doesnt 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;
            }
        }
    }
}

无论如何,我想知道是否有人会对此有更多的了解,并能给我一些启发.

Anyhow I am wondering if anyone might have a little more knowledge on how to do this and could shed some light on me.

推荐答案

像您提到的那样,Picker中没有可用的TextColor属性.

There is no TextColor property available in the Picker like you mention.

即使是这种情况,我们仍然可以实现为WindowsPhone更改Picker文本颜色.

Even this being the case, we can still achieve changing the Picker text color for WindowsPhone.

我假设您是从PickerRenderer继承的,因为您的代码示例中缺少它,并且我添加了一些额外的东西,所以这对其他人更有用:-

I'm assuming you are inheriting from PickerRenderer as it was missing from your code example and I've added some extra things so this is more helpful to others:-

PCL中定义接口:-

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渲染器:-

Create your WindowsPhone renderer like so in a 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));
    }

请注意,如果您只想设置一次文本颜色,那么以上就是您所需要的.

Note, the above is all what you need if you just want to set the text color the once.

但是,如果要在初始设置后更改颜色,则您将需要收听属性更改并按照以下步骤对其进行操作:-

However if you want to change the color after it has been initially set, then you will need to listen to the property change and act upon it like 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 as well:-

[assembly: ExportRendererAttribute(typeof(CustomPicker2), typeof(CustomPicker2Renderer))]

这篇关于在Windows Phone 8.1的Xamarin Forms中更改Picker控件的默认文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆