【问题标题】:How to change Border Color of Entry in Xamarin.Forms [duplicate]如何在 Xamarin.Forms 中更改条目的边框颜色 [重复]
【发布时间】:2016-10-15 19:33:35
【问题描述】:

我正在用 Xamarin.forms 跨平台编写应用程序。 该应用程序中的条目很少,我想创建/将边框颜色更改为红色。 有什么简单的方法可以做到这一点吗?或者有什么方法存在吗?

【问题讨论】:

    标签: xaml xamarin cross-platform xamarin.forms xamarin-studio


    【解决方案1】:

    我认为您只能使用 CustomRenderer 来实现这一点:

    iOS:

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
           base.OnElementPropertyChanged(sender, e);
    
           Control.Layer.BorderColor = UIColor.Red.CGColor;
           Control.Layer.BorderWidth = 1;
    }
    

    在 Android 上,我认为没有 CustomRender 是不可能的(实际上,如果是的话……我不知道怎么做~对不起):

    使用 CustomRenderer 是这样的:

        [assembly: ExportRenderer(typeof(Entry), typeof(SuperEntryRenderer))]
        namespace Bla{
        public class SuperEntryRenderer : EntryRenderer
            {
                protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
                {
                    base.OnElementChanged(e);
                    if (e.OldElement == null)
                    {
                        var nativeEditText = (global::Android.Widget.EditText)Control;
                        var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());
                        shape.Paint.Color = Xamarin.Forms.Color.Red.ToAndroid();
                        shape.Paint.SetStyle(Paint.Style.Stroke);
                        nativeEditText.Background = shape;
                    }
                }
            }
    

    【讨论】:

    • 谢谢 Rodrigo,这对刚刚研究自定义渲染器的人来说非常有帮助。我很惊讶要改变android上的边框颜色需要做多少工作。
    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多