我的 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/