【问题标题】:How to remove visual material entry underline如何删除视觉材料条目下划线
【发布时间】:2021-08-27 13:27:16
【问题描述】:

我们正在为我们的项目使用视觉材料条目。

using Xamarin.Forms.Material.Android;

[assembly: ExportRenderer(typeof(ProgressBar), typeof(CustomMaterialProgressBarRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]
namespace MyApp.Android
{
    public class CustomMaterialProgressBarRenderer : MaterialProgressBarRenderer
    {
        //...
    }
}

如何去除材料条目下划线?

【问题讨论】:

  • 一般来说,通过自定义渲染器进行调整的答案是:1) 找出用于表示该视图的原生类。 2)查看该本地类上可用的方法。希望您会发现平台类有办法禁用该下划线。

标签: c# android xamarin xamarin.forms custom-renderer


【解决方案1】:
  1. 创建一个dimension resource(添加一个新的.xml文件并将其保存在Resources\values的Android项目下)
<?xml version="1.0" encoding="utf-8"?>
<resources> 
<dimen name="box_stroke_dim">0dp</dimen>
</resources>
  1. 每个EntryVisual="Material" 的自定义渲染器实现
[assembly: ExportRenderer(typeof(Entry), typeof(App.Droid.MyMaterialEntryRenderer),
            new[] { typeof(VisualMarker.MaterialVisual) })]

namespace App.Droid
{
    public class MyMaterialEntryRenderer : MaterialEntryRenderer
    {
        public MyMaterialEntryRenderer(Context context) : base(context)
        {
        }

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

            Control?.SetBoxStrokeWidthResource(Resource.Dimension.box_stroke_dim);
            Control?.SetBoxStrokeWidthFocusedResource(Resource.Dimension.box_stroke_dim);
        }
    }
}

【讨论】:

  • 谢谢。这工作正常。这个ios怎么做?
  • 对不起,我现在不知道 iOS 部分,如果我想出一些我会更新的东西,或者其他人可以为 iOS 提供答案
【解决方案2】:

您可以使用custom renderersmaterial visual 来实现条目下划线去除。 我正在使用下面的代码将其应用于项目中的所有条目,并且它正在使用 Xamarin Forms 4.8+

Xamarin Android

条目

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2016-04-04
    • 2020-05-19
    相关资源
    最近更新 更多