问题描述
我们正在为我们的项目使用视觉材料条目。
using Xamarin.Forms.Material.IOS;
[assembly: ExportRenderer(typeof(Entry),typeof(CustomMaterialEntryRenderer),new[] { typeof(VisualMarker.MaterialVisual) })]
namespace MyApp.Android
{
public class CustomMaterialEntryRenderer : MaterialProgressBarRenderer
{
//...
}
}
如何去除xamarin表单ios中的材料条目下划线?
解决方法
您可以将活动下划线高度设置为 0f
以实现此目的:
[assembly: ExportRenderer(typeof(CustomMaterialEntry),typeof(CustomMaterialEntryRenderer),new[] { typeof(VisualMarker.MaterialVisual) })]
namespace MyApp.iOS
public class CustomMaterialEntryRenderer : MaterialEntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (this.Control != null)
{
Control.ActiveTextInputController.UnderlineHeightActive = 0f;
}
}
}
,
您可以使用 custom renderers 和 material visual 来实现条目下划线的去除。 我正在使用以下代码将其应用于项目中的所有条目,并且它正在与 Xamarin Forms 4.8+ 一起使用
Xamarin 安卓
条目
[assembly: ExportRenderer(typeof(Entry),typeof(EntryMaterialRendererAndroid),new[] { typeof(VisualMarker.MaterialVisual) })]
namespace XFTest.Droid.Renderers
{
public class EntryMaterialRendererAndroid : MaterialEntryRenderer
{
public EntryMaterialRendererAndroid(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.BoxStrokeWidth = 0;
Control.BoxStrokeWidthFocused = 0;
}
}
}
}
Xamarin iOS
条目
[assembly: ExportRenderer(typeof(Entry),typeof(EntryMaterialRendereriOS),new[] { typeof(VisualMarker.MaterialVisual) })]
namespace XFTest.iOS.Renderers
{
public class EntryMaterialRendereriOS : MaterialEntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
EntryRemoveUnderLine();
}
protected override void OnElementPropertyChanged(object sender,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender,e);
EntryRemoveUnderLine();
}
protected void EntryRemoveUnderLine()
{
if (Control != null)
{
Control.BorderStyle = UITextBorderStyle.None;
Control.Underline.Enabled = false;
Control.Underline.DisabledColor = UIColor.Clear;
Control.Underline.Color = UIColor.Clear;
Control.Underline.BackgroundColor = UIColor.Clear;
Control.ActiveTextInputController.UnderlineHeightActive = 0f;
Control.PlaceholderLabel.BackgroundColor = UIColor.Clear;
}
}
}
}