我正在 Android
上创建一个 Xamarin.Forms
应用程序,我正在尝试更改我的 Xamarin.Forms
下面线条的颜色入口
控制。
我有一个像这样的 Entry
控件:
<Entry Text="new cool street"/>
我想将此 Entry
下方的线条颜色从默认的白色更改为更偏紫色以匹配我的主题。
理想情况下,最好使用 Android 样式,因为如果可能,它会应用于从 Entry
继承的所有控件
这可能吗?
最佳答案
您可以使用会影响所有条目的自定义渲染器,
这里是安卓的:
[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace Android.MyRenderers
{
public class MyEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null) return;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
Control.BackgroundTintList = ColorStateList.ValueOf(Color.White);
else
Control.Background.SetColorFilter(Color.White, PorterDuff.Mode.SrcAtop);
}
}
}
和 iOS:
[assembly: ExportRenderer (typeof(Entry), typeof(MyEntryRenderer))]
namespace iOS.MyRenderers
{
public class MyEntryRenderer : EntryRenderer
{
private CALayer _line;
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
_line = null;
if (Control == null || e.NewElement == null)
return;
Control.BorderStyle = UITextBorderStyle.None;
_line = new CALayer {
BorderColor = UIColor.FromRGB(174, 174, 174).CGColor,
BackgroundColor = UIColor.FromRGB(174, 174, 174).CGColor,
Frame = new CGRect (0, Frame.Height / 2, Frame.Width * 2, 1f)
};
Control.Layer.AddSublayer (_line);
}
}
}
不确定 Windows 解决方案
关于c# - 是否可以更改下方线条的颜色/文本框的边框(条目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38207168/