删除Xamarin表格中输入控件的下划线

yduiuuwa  于 2023-03-10  发布在  其他
关注(0)|答案(6)|浏览(140)

我是新的Xamarin-Forms和开发一个登录表单和使用材料设计(IVisual)。我已经创建了一个自定义的条目类,并继承了它与MaterialEntryRenderer自定义它。我想实现的事情是删除下划线Entry有。我看到了很多例子,但他们都使用EntryRenderer代替。

public class CustomEntryRenderer : MaterialEntryRenderer
{
    public CustomEntryRenderer(Context context) : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
           Control.Background = null;
           Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
        }
    }
}

它在EntryRenderer上工作良好,但在MaterialEntryRenderer上就不行了。

5t7ly7z5

5t7ly7z51#

试试这个,对我很有效

Control.EditText.Background = null;
Control.EditText.SetBackgroundColor(Android.Graphics.Color.Transparent);
kmpatx3s

kmpatx3s2#

创建一个效果并将其应用到控件中,我刚刚写了一篇详细的文章,介绍如何在这里简单的步骤:How to remove android entry underline

rjzwgtxy

rjzwgtxy3#

在Xamarin Forms Visual Material 4.8+的新版本中,我使用了下面的代码,它在Android和iOS上运行良好。

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;
                // Remove underline on focus
                Control.ActiveTextInputController.UnderlineHeightActive = 0f;
                // Remove placeholder background
                Control.PlaceholderLabel.BackgroundColor = UIColor.Clear;
            }
        }
    }
}
smdncfj3

smdncfj34#

我正在使用Xamarin 5型版本(5.0.0.2291)
你可以跟着这个
https://kudzaidube.medium.com/xamarin-forms-how-to-remove-android-entry-underline-4628a8d1f058
把这一行改一下
安卓.图形.颜色输入LineColor =安卓.图形.颜色.白色;

安卓.图形.颜色输入LineColor =新安卓.图形.颜色(0,0,0,0);
为了使用透明色

eivnm1vs

eivnm1vs5#

您可以删除它,只需使用Xamarin.CommunityToolkit

外观:]

xmlns:tool="http://xamarin.com/schemas/2020/toolkit"

<Entry.Effects>
    <tool:RemoveBorderEffect />
</Entry.Effects>
wswtfjt7

wswtfjt76#

尝试更改“文字颜色”特性。
据消息人士称,它会改变你想要的:

_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);

查看:https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Material.Android/MaterialEntryRenderer.cs

相关问题