【问题标题】:CustomRenderer to dynamically change border colorCustomRenderer 动态改变边框颜色
【发布时间】:2018-05-11 22:52:06
【问题描述】:

我有一个Entry,如果条目为空,我想在按下按钮时在其周围添加一个红色边框。因此我需要能够动态地改变颜色。 (标准验证器)

xaml:

<local:BorderChange Placeholder="Example Entry" BorderColor="#ff4444"></local:BorderChange>

PCL 控制:

namespace Project
{
    public class BorderChange : Entry
    {
        public string BorderColor
        {
            get; 
            set;
        }
    }
}

iOS 自定义渲染器:

[assembly: ExportRenderer(typeof(BorderChange), typeof(BorderColorChange))]
namespace Project.iOS
{
    public class BorderColorChange : EntryRenderer
    {
    //init color

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

            if(Control != null)
            {
                Control.Layer.BorderColor = UIColor.Blue; //This is where i want to add my color
            }
        }
    }
} 
  • 如何将我的属性传递给 CustomRenderer,以便动态更改 BorderColor 参数?

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios


    【解决方案1】:

    满足此类需求的更好方法是使用Effects

    Here 是一个很好的示例,说明了如何使用参数作为附加属性创建效果。您将能够从您的视图模型中绑定一个属性(假设IsValid 直接绑定到效果的附加属性)。

    【讨论】:

      猜你喜欢
      • 2011-10-19
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 2017-09-16
      • 2020-11-04
      • 1970-01-01
      相关资源
      最近更新 更多