【问题标题】:Xamarin Forms : Hide Editor or Entry underlineXamarin 表单:隐藏编辑器或条目下划线
【发布时间】:2021-07-28 23:22:35
【问题描述】:

我在 UI 中使用编辑器。但是黑色下划线很烦人,有什么办法可以去掉那个下划线吗?

提前致谢

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    在您的 Android 项目中创建自定义渲染器:

    public class CustomEntryRenderer : EntryRenderer {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) {
            base.OnElementChanged(e);
            Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
        }
    }
    

    然后在你的 XAML 中:

    <ContentPage xmlns:xyz="clr-namespace:PclNamespaceForCustomControls">
        ...
        <xyz:CustomEntry Placeholder="Lorem ipsum" />
    </ContentPage>
    

    有关详细信息,请参阅 Microsoft 文档:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry

    【讨论】:

    • 多年来我见过的最干净的代码。我刚刚将它粘贴到我的项目中,我的产品现在热销。
    【解决方案2】:

    我已经使用custom renders 完成了条目和编辑器下划线删除功能。

    ##在 PCL##

    CustomEntry.cs

    using Xamarin.Forms;
    
    namespace Entry_Editor_Sample
    {
        public class CustomEntry : Entry
        {
        }
    }
    

    CustomEditor.cs

    using Xamarin.Forms;
    
    namespace Entry_Editor_Sample
    {
        public class CustomEditor : Editor
        {
        }
    }
    

    ##在安卓中##

    CustomEntryRenderer.cs

    using Android.Content;
    using Android.Content.Res;
    using Android.Graphics.Drawables;
    using Android.Text;
    using Entry_Editor_Sample;
    using Entry_Editor_Sample.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
    
    namespace Entry_Editor_Sample.Droid
    {
        class CustomEntryRenderer : EntryRenderer
        {
            public CustomEntryRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    GradientDrawable gd = new GradientDrawable();
                    gd.SetColor(global::Android.Graphics.Color.Transparent);
                    this.Control.SetBackgroundDrawable(gd);
                    this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);
                    //Control.SetHintTextColor(ColorStateList.ValueOf(global::Android.Graphics.Color.White));
                }
            }
        }
    }
    

    CustomEditorRenderer.cs

    using Android.Content;
    using Android.Content.Res;
    using Android.Graphics.Drawables;
    using Android.Text;
    using Entry_Editor_Sample;
    using Entry_Editor_Sample.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
    
    namespace Entry_Editor_Sample.Droid
    {
        class CustomEditorRenderer : EditorRenderer
        {
            public CustomEditorRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    GradientDrawable gd = new GradientDrawable();
                    gd.SetColor(global::Android.Graphics.Color.Transparent);
                    this.Control.SetBackgroundDrawable(gd);
                    this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);
                    //Control.SetHintTextColor(ColorStateList.ValueOf(global::Android.Graphics.Color.Black));
                }
            }
        }
    }
    

    ##在IOS中##

    CustomEntryRenderer.cs

    using Entry_Editor_Sample;
    using Entry_Editor_Sample.iOS;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
    
    namespace Entry_Editor_Sample.iOS
    {
        class CustomEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    Control.BorderStyle = UITextBorderStyle.None;
                    Control.Layer.CornerRadius = 10;
                    //Control.TextColor = UIColor.Black;
                }
            }
        }
    }
    

    CustomEditorRenderer.cs

    using Entry_Editor_Sample;
    using Entry_Editor_Sample.iOS;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
    
    namespace Entry_Editor_Sample.iOS
    {
        class CustomEditorRenderer : EditorRenderer
        {
            public CustomEditorRenderer()
            {
                UIKeyboard.Notifications.ObserveWillShow((sender, args) =>
                {
                    if (Element != null)
                    {
                        Element.Margin = new Thickness(0, 0, 0, args.FrameEnd.Height); //push the entry up to keyboard height when keyboard is activated
                    }
                });
    
                UIKeyboard.Notifications.ObserveWillHide((sender, args) =>
                {
                    if (Element != null)
                    {
                        Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed
                    }
                });
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    Control.Layer.CornerRadius = 10;
                    Control.TextColor = UIColor.Black;
                }
            }
        }
    }
    

    ##Finally 在 MainPage.xaml##

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:local="clr-namespace:Entry_Editor_Sample"
                 mc:Ignorable="d"
                 BackgroundColor="White"
                 xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
                 ios:Page.UseSafeArea="true"
                 x:Class="Entry_Editor_Sample.MainPage">
    
        <StackLayout
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="FillAndExpand"
            BackgroundColor="White">
    
            <local:CustomEntry
                BackgroundColor="SkyBlue"
                Placeholder="Entry"
                PlaceholderColor="Black"
                TextColor="Black"/>
    
            <local:CustomEditor
                BackgroundColor="SkyBlue"
                Placeholder="Editor"
                HeightRequest="100"
                PlaceholderColor="Black"
                TextColor="Black"/>
        </StackLayout>
    </ContentPage>
    

    我在here 上上传了一个样本:)

    【讨论】:

    • @DanielDolz 请评论渲染器上的提示文本颜色代码。也更新了答案,请检查。
    【解决方案3】:

    您可以使用custom renderers来实现条目和编辑器下划线去除。 我正在使用以下代码将这些功能应用于项目中的所有条目和编辑器,并且它正在使用 Xamarin Forms 4.8+

    Xamarin Android

    条目

    [assembly: ExportRenderer(typeof(Entry), typeof(EntryRendererAndroid), new[] { typeof(VisualMarker.DefaultVisual) })]
    namespace XFTest.Droid.Renderers
    {
        public class EntryRendererAndroid : EntryRenderer
        {
            public EntryRendererAndroid(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);
                }
            }
        }
    }
    

    编辑器

    [assembly: ExportRenderer(typeof(Editor), typeof(EditorRendererAndroid), new[] { typeof(VisualMarker.DefaultVisual) })]
    namespace XFTest.Droid.Renderers
    {
        public class EditorRendererAndroid : EditorRenderer
        {
            public EditorRendererAndroid(Context context) : base(context) { }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    Control.Background = null;
                    Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
                }
            }
        }
    }
    

    Xamarin iOS

    条目

    [assembly: ExportRenderer(typeof(Entry), typeof(EntryRendereriOS), new[] { typeof(VisualMarker.DefaultVisual) })]
    namespace XFTest.iOS.Renderers
    {
        public class EntryRendereriOS : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    Control.BackgroundColor = UIColor.FromWhiteAlpha(1, 1);
                    Control.Layer.BorderWidth = 0;
                    Control.BorderStyle = UITextBorderStyle.None;
                }
            }
        }
    }
    

    编辑器

    [assembly: ExportRenderer(typeof(Entry), typeof(EntryRendereriOS), new[] { typeof(VisualMarker.DefaultVisual) })]
    namespace XFTest.iOS.Renderers
    {
        public class EntryRendereriOS : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    Control.BackgroundColor = UIColor.FromWhiteAlpha(1, 1);
                    Control.Layer.BorderWidth = 0;
                    Control.BorderStyle = UITextBorderStyle.None;
                }
            }
        }
    }
    

    【讨论】:

    • 如果您觉得答案有帮助,请将其标记为已接受的答案并投票,这将对其他会员有所帮助。
    • 我建议不要只是复制粘贴,iOS 中的 Editor 渲染器是 Entry 渲染器的副本。 BorderStyle 不适用于编辑器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多