有什么方法可以使用 material visual 在 iOS 中自定义控件样式吗?

is there any way to customise a control style in iOS using material visual?

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

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

[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();
        }
    }

为了清楚起见,以防万一,我想要红色的底线和黑色的文本。

提前致谢!

CustomRenderer 永远不会被调用似乎是一个存在的问题。我们将重点关注这个问题。

解决方法 1:

如果你只想设置Entry的下划线颜色。不需要设置Visual=Material。你只需要创建一个默认的Custom Renderer of 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 中设置 WidthRequestHeightRequest

解决方法 2

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

从 Nuget 管理器下载包(确保检查 include prerelease)

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

<local:MaterialEntry IsPassword="True" Placeholder="email" AccentColor="Red"/>