如何使用Xamarin表单从条目控件中删除边框 [英] How to remove the border from a entry control with xamarin forms

查看:178
本文介绍了如何使用Xamarin表单从条目控件中删除边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用xamarin控件从输入框中删除边框,以用于以下控件.

How to remove the border from a entry box with xamarin control for the following control.

<Entry Text="" />

目前,我在文本框上看到一个细边框,在这里看不到要删除的任何border属性.

Currently I'm seeing a thin border against the textbox, where i'm not seeing any border property to remove.

请让我知道如何禁用此功能.

Please let me know, how to disable this.

推荐答案

有些控件的属性无法通过Xamarin.Forms进行操作,因此您必须实现

There some properties of controls that you cannot manipulate via Xamarin.Forms, you'll have to implement either an effect or a custom renderer. An effect might well do in your case, but since I'm more proficient with custom renderers, I'll show you how to achieve what you want with a custom renderer.

您必须创建一个从EntryRenderer派生的类,该类将覆盖OnElementChanged

You'll have to create a class deriving from EntryRenderer that overrides OnElementChanged

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

        this.Control.LeftView = new UIView(new CGRect(0, 0, 8, this.Control.Frame.Height));
        this.Control.RightView = new UIView(new CGRect(0, 0, 8, this.Control.Frame.Height));
        this.Control.LeftViewMode = UITextFieldViewMode.Always;
        this.Control.RightViewMode = UITextFieldViewMode.Always;

        this.Control.BorderStyle = UITextBorderStyle.None;
        this.Element.HeightRequest = 30;
    }
}

首先,通过设置本机控件的LeftViewRightView,向控件中添加了一些填充(否则看起来很难看).无论如何,通过设置本机控件的此属性,更有趣的部分是BorderStyle,您可以删除控件的边框.

First there are some paddings added to the control (it looks quite ugly otherwise) by setting the LeftView and the RightView of the native control. Anyway, the more interesting part ist the BorderStyle by setting this property of the native control you can remove the border of the control.

最后要做的就是说Xamarin.Forms使用该渲染器.在文件的全局范围内(在名称空间声明之外)使用以下属性:

Last thing you'll have to do is to say Xamarin.Forms to use that renderer. Use the following attribute in the global scope of your file (out of the namespace declaration):

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]

如果您不希望将样式应用于所有条目,则必须在Xamarin.Forms项目中定义一个从Entry派生的CustomEntry类.上面显示的行

If you don't want the style to be applied to all entries, you'll have to define a CustomEntry class that derives from Entry in your Xamarin.Forms project change the line presented above to

[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]

请注意:这是iOS实现,但对于Android基本上相同.

Please note: This is the iOS implementation, but basically it's the same for Android.

这篇关于如何使用Xamarin表单从条目控件中删除边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆