use*_*er1 14 c# android xamarin xamarin.forms
我正在创建一个Xamarin.Forms应用程序Android,我正在尝试更改我Xamarin.Forms Entry控件下面的行的颜色.
我有这样的Entry控件:
<Entry Text="new cool street"/>
Run Code Online (Sandbox Code Playgroud)
我想将此下方线条的颜色Entry从默认白色更改为更多紫色以匹配我的主题.
理想情况下,使用Android样式会更好,因为它适用于从Entry可能的地方继承的所有控件
这可能吗?
roo*_*oot 31
您可以使用会影响所有条目的自定义渲染器,
这是Android的:
[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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
不确定Windows解决方案
Cur*_*ity 10
我有同样的问题,只是更改styles.xml中的colorAccent值(在Xamarin.Android项目中)将更改光标的颜色和字段的下边框.Entry
<item name="colorAccent">#BA55D3</item>
Run Code Online (Sandbox Code Playgroud)
由于我的内容页面具有一种背景色,而对话框具有另一种背景色,因此使用样式指定底部条形颜色完全是错误的答案。由于OP仅询问Android,因此这只是Android ...
我使用自定义渲染器将底部栏颜色设置为与文本颜色相同。必须同时具有ElementChanged和PropertyChanged。
[assembly: ExportRenderer(typeof(Xamarin.Forms.Entry), typeof(CustomEntryRenderer))]
namespace XamFormsConnect.Droid
{
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Entry> e)
{
base.OnElementChanged(e);
if (Control != null && e.NewElement != null)
{
var entry = (Xamarin.Forms.Entry)e.NewElement;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
Control.BackgroundTintList = ColorStateList.ValueOf(entry.TextColor.ToAndroid());
else
Control.Background.SetColorFilter(entry.TextColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == "TextColor")
{
var entry = (Xamarin.Forms.Entry)sender;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
Control.BackgroundTintList = ColorStateList.ValueOf(entry.TextColor.ToAndroid());
else
Control.Background.SetColorFilter(entry.TextColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了最受好评的解决方案,但出现错误: \xe2\x80\x9cContext 自版本 2.5 起已过时。请使用本地上下文\xe2\x80\x9d。
\n经过一些快速搜索:
\n代码需要更新:
\nusing <YOUR_APP_NAME>.Droid;\nusing Xamarin.Forms;\nusing Xamarin.Forms.Platform.Android;\nusing Android.Content;\nusing Android.OS;\nusing Android.Content.Res;\nusing Android.Graphics;\n\n[assembly: ExportRenderer(typeof(Entry), typeof(MyRenderers))]\nnamespace Android.MyRenderers\n{\n public class MyRenderers : EntryRenderer\n {\n\n public MyRenderers(Context context) : base(Android.App.Application.Context)\n {\n\n }\n protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)\n {\n base.OnElementChanged(e);\n\n if (Control == null || e.NewElement == null) return;\n\n if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)\n Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.Ivory);\n else\n Control.Background.SetColorFilter(Android.Graphics.Color.Ivory, PorterDuff.Mode.SrcAtop);\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n对于需要更新的人。
\n