1

我正在尝试为启用Entry的 iOS 平台自定义一个字段。Visual=Material

我尝试过,CustomRenderer但由于是 iOS 平台,我不知道如何达到,例如,在不修改控件的整个文本颜色的情况下修改材质底部边框颜色。

[assembly: ExportRenderer(typeof(Entry), typeof(CustomMaterialEntryRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]

    public class CustomMaterialEntryRenderer : MaterialEntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null || e.NewElement == null) return;

            Layer.BorderColor = Color.FromHex("#cedee7").ToCGColor();
        }
    }

为了以防万一,我希望底线为红色,文本为黑色。

条目示例

提前致谢!

4

1 回答 1

1

似乎存在的问题CustomRenderer永远不会被调用。我们将专注于这个问题。

解决方法 1:

如果你只想设置下划线颜色Entry。无需设置Visual=Material。您只需要创建一个默认的自定义渲染Entry

if (Control != null)
{

  Control.BorderStyle = UITextBorderStyle.None;
 
  UIView lineView = new UIView()
  {

     Frame = new CGRect(0, Element.HeightRequest - 1, Element.WidthRequest, 1),
     BackgroundColor = UIColor.Red,

  };
 
  Control.AddSubview(lineView);
 
}

不要忘记在 xaml中设置WidthRequestand 。HeightRequest

解决方法 2

幸运的是,nuget 有很多 Material Controls 插件。并且可以直接下载使用。例如MaterialFormControls

从 Nuget Manager 下载包(确保检查包含预发布) 在此处输入图像描述

并设置属性 AccentColor 以更改下划线颜色

<local:MaterialEntry IsPassword="True" Placeholder="email" AccentColor="Red"/>
于 2019-08-28T11:55:22.603 回答