输入打字颜色

时间:2016-10-16 03:46:57

标签: c# uwp xamarin.forms

我在PCL部分有CustomEntry

public class CustomEntry : Entry
{
    /// <summary>
    /// The HasBorder property.
    /// </summary>
    public static readonly BindableProperty HasBorderProperty =
        BindableProperty.Create(nameof(HasBorder), typeof(bool), typeof(CustomEntry), true);

    /// <summary>
    /// Assessor for the HasBorder property.
    /// </summary>
    public bool HasBorder
    {
        get { return (bool)GetValue(HasBorderProperty); }
        set { SetValue(HasBorderProperty, value); }
    }

    /// <summary>
    /// The XAlign property
    /// </summary>
    public static readonly BindableProperty XAlignProperty =
        BindableProperty.Create("XAlign", typeof(TextAlignment), typeof(CustomEntry),
        TextAlignment.Start);

    /// <summary>
    /// Gets or sets the X alignment of the text
    /// </summary>
    public TextAlignment XAlign
    {
        get { return (TextAlignment)GetValue(XAlignProperty); }
        set { SetValue(XAlignProperty, value); }
    }
}

然后是UWP部分的渲染器:

public class CustomEntryRenderer : EntryRenderer
{
    /// <summary>
    /// Instance of our Custom control declared in the PCL part.
    /// </summary>
    CustomEntry customEntry;

    /// <summary>
    /// We override the OnElementChanged() event handler to get the desired instance. We also use it for updates.
    /// </summary>
    /// <param name="e">It contains either the NewElement or the OldElement.</param>
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            customEntry = e.NewElement as CustomEntry;

            if (customEntry != null)
            {
                SetTextAlignment();
                SetBorderPresence();
                SetTextColor();
            }
        }
    }


    /// <summary>
    /// The on element property changed callback.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="PropertyChangedEventArgs"/>Instance containing the event data.</param>
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == CustomEntry.XAlignProperty.PropertyName)
            SetTextAlignment();
        else if (e.PropertyName == CustomEntry.HasBorderProperty.PropertyName)
            SetBorderPresence();
        else if (e.PropertyName == CustomEntry.TextColorProperty.PropertyName)
            SetTextColor();
    }

    /// <summary>
    /// Sets the text alignment.
    /// </summary>
    /// <param name="view">The view.</param>
    private void SetTextAlignment()
    {
        switch (customEntry.XAlign)
        {
            case Xamarin.Forms.TextAlignment.Center:
                Control.TextAlignment = Windows.UI.Xaml.TextAlignment.Center;
                break;
            case Xamarin.Forms.TextAlignment.End:
                Control.TextAlignment = Windows.UI.Xaml.TextAlignment.Right;
                break;
            case Xamarin.Forms.TextAlignment.Start:
                Control.TextAlignment = Windows.UI.Xaml.TextAlignment.Left;
                break;
        }
    }

    /// <summary>
    /// Sets the border presence.
    /// </summary>
    private void SetBorderPresence()
    {
        if (!customEntry.HasBorder)
        {
            Control.BorderThickness = new Windows.UI.Xaml.Thickness();
        }
    }

    /// <summary>
    /// Set the Text color.
    /// </summary>
    private void SetTextColor()
    {
        /*Control.ForegroundFocusBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(255,
            Convert.ToByte(customEntry.TextColor.R),
            Convert.ToByte(customEntry.TextColor.G),
            Convert.ToByte(customEntry.TextColor.B)));

        Brush tmp = (SolidColorBrush)new ColorConverter().Convert(customEntry.TextColor, null, null, null);

        Control.ForegroundFocusBrush = (SolidColorBrush)new ColorConverter().Convert(customEntry.TextColor, null, null, null);
        Debug.WriteLine("Pass");*/
    }
}

问题是,当我打字时,textColor是黑色的,意味着我不会想我正在打字,因为它在黑暗的设计上是黑色的。有人知道如何改变打字的颜色吗?因为当我解散de Entry时,文本变成白色,就像我想要的那样......

有XAML声明:

        <AbsoluteLayout WidthRequest="{Binding EntryWidth}" HeightRequest="{Binding EntryHeight}">
          <control:CustomEntry Placeholder="pseudo" PlaceholderColor="Gray"
                               Text="" FontFamily="{extension:FontFamily Roboto_Light}" FontSize="20" TextColor="White"
                               XAlign="Center" HasBorder="false" BackgroundColor="Transparent"
                               AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                               AbsoluteLayout.LayoutFlags="All"/>
          <BoxView BackgroundColor="{StaticResource NL_OrangeBeer}"
                   AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 0.02"
                   AbsoluteLayout.LayoutFlags="All"/>
        </AbsoluteLayout>

1 个答案:

答案 0 :(得分:1)

我不知道你是否解决了这个问题,但我最近遇到了同样的问题。

因此,在自定义渲染器中,Control上有一个名为ForegroundFocusBrush的属性,可以在聚焦时设置颜色。

只需在OnElementChanged覆盖中执行此操作,

例如,将聚焦颜色设置为白色

Control.ForegroundFocusBrush = new SolidColorBrush(Colors.White);