c# - 更改焦点条目的边框颜色

标签 c# android xamarin xamarin.forms

我的 Xamarin.Forms Android 应用有一个自定义条目。目前,我正在使用自定义渲染器为条目提供带边框的椭圆形。

我还想在聚焦时更改 Entry Border 的颜色,并在未聚焦时恢复为原始颜色。

我的自定义渲染器如下:

public class EntryCellRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        var UIEntry = (e.NewElement != null) ? (EntryCell)e.NewElement : (EntryCell)e.OldElement;

        if (this.Control != null)
        {
            Control.Gravity = Android.Views.GravityFlags.CenterVertical;
            Control.SetPadding(30, 30, 30, 31);
            GradientDrawable gd = new GradientDrawable();
            gd.SetShape(ShapeType.Rectangle);

            var BackgroundColor = ColorHelper.FromHex(CoreTheme.COLOR_DEFAULT_CLEAR);
            var ColorRef = Android.Graphics.Color.Argb(
                (byte)(BackgroundColor.A * 255),
                (byte)(BackgroundColor.R * 255),
                (byte)(BackgroundColor.G * 255),
                (byte)(BackgroundColor.B * 255));


            gd.SetColor((int)ColorRef);

            UIEntry.BackgroundColor = Xamarin.Forms.Color.Transparent;
            gd.SetStroke(7, Android.Graphics.Color.LightGray);
            gd.SetCornerRadius(20.0f);
            Control.SetBackground(gd);

        }
    }
}

我不确定如何继续为上面我想用这个自定义条目执行的操作设置焦点事件。

最佳答案

在您的 OnElementChanged 中,您可以将一个事件连接到 Focused 事件:

e.NewElement.Unfocused += (sender, evt) =>
{
    // unfocused, set color
};
e.NewElement.Focused += (sender, evt) =>
{
    // focus, set color
};

仅供引用:OnNativeFocusChanged 将是通过覆盖执行此操作的完美位置,但它不是公开的...

internal virtual void OnNativeFocusChanged(bool hasFocus)
{
}

关于c# - 更改焦点条目的边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40109732/

相关文章:

c# - 并非所有代码路径都返回 Array 的值

android - GreenDao无法设置数据库

java - 如何删除 Android 网格布局之间的空间

xamarin - DisplayAlert 导致 Xamarin 应用崩溃

c# - 嵌套 foreach 的 Lambda 表达式

c# - Collat​​z 序列 - 减去错误

android - 将位图保存为 jpeg 文件

c# - Windows 10 移动版不遵守 KeyboardFlags.None

xamarin - 如何使 UWP 应用与 Windows 8.1 兼容

c# - 我如何判断一个类是否可以序列化?