输入颜色

本文关键字:颜色 输入 | 更新日期: 2023-09-27 18:13:11

我在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是黑色的,意思是我不认为我在打字,因为它是黑色的设计。有人知道怎么改变打字的颜色吗?因为当我取消对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>

输入颜色

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

在你的自定义渲染器中,Control上有一个名为ForegroundFocusBrush的属性它在聚焦时设置颜色

只需在OnElementChanged重写中执行此操作,

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

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