c# - 更改 Xamarin Forms for Windows Phone 8.1 中 Picker 控件的默认文本颜色

标签 c# windows-phone-8 xamarin xamarin.forms

我正在使用 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/

相关文章:

c# - Delegate.BeginInvoke 与 ThreadPool.QueueWorkerUserItem

c# - 自动完成方法括号

c# - Visual Studio 2012 运行 Windows Phone 问题

windows-8 - 我可以将我的 Windows Phone 和 Windows 8 应用程序从一个帐户转移到另一个帐户吗?

android - OnTokenRefresh 从未在 Xamarin Android 中调用

c# - 如果我只想为 Xamarin Studio 使用命令行,应该选择哪种项目类型?

c# - 如何在 Android 项目的 Visual Studio for Mac 中将 'EmbedAssembliesIntoApk' MSBuild 属性设置为 'true'?

c# - HttpContext GetLocalResourceObject 和 GetGlobalResourceObject 之间的区别

c# - 在 C# 中使用 List<T>(泛型)

c# - Async-Await 与 Task<T>.Result on WP8