繁体   English   中英

如何从 iOS 自定义渲染器访问 PCL 渲染器中的 BindableProperty

[英]How to access BindableProperty in PCL renderer from iOS customrenderer

我试图访问我尝试为其编写渲染器代码的自定义按钮的可绑定属性。 首先是我的 PCL 渲染器:

public class BtnRenderer : Button
{

    public static readonly BindableProperty HighLightProperty = BindableProperty.Create(nameof(HighlightedBackgroundColor), typeof(Color), typeof(BtnRenderer), default(Color));

    public Color HighlightedBackgroundColor
    {
        get
        {
            return (Color)GetValue(HighLightProperty);
        }
        set
        {
            SetValue(HighLightProperty, value);
        }
    }
}

如您所见,我打算从 XAML 设置一个HighlightedBackgroundColor ,但是,我不知道如何在我的 iOS 渲染器中访问它,我所拥有的是:

[assembly: ExportRenderer(typeof(BtnRenderer), typeof(BtnRendereriOS))]
namespace BluetoothExample.iOS
{
    public class BtnRendereriOS : ButtonRenderer
    {

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

            if (Control != null)
            {
                var normalBackgroundColor = Element.BackgroundColor.ToUIColor();
                var _highlightBackgroundColor = Element.HighlightedBackgroundColor.ToUIColor(); //HERE IS MY PROBLEM

            async Task NormalColorState(UIButton button)
            {
                await UIView.TransitionNotifyAsync(button, .25, UIViewAnimationOptions.TransitionCrossDissolve, () =>
                {
                    button.BackgroundColor = normalBackgroundColor;
                });
            }
            Control.TouchDown += async (object sender, EventArgs c) =>
            {
                var button = sender as UIButton;
                await UIView.TransitionNotifyAsync(button, .25, UIViewAnimationOptions.TransitionCrossDissolve, () =>
                {
                    button.BackgroundColor = _highlightBackgroundColor;
                });
            };
        }
    }
}

如何正确访问此属性?

//这是我的问题

var _highlightBackgroundColor = Element.HighlightedBackgroundColor.ToUIColor();

直接使用Element是渲染器的基础( VisualElementRenderer<TElement> ),因此为了访问子类上的任何自定义属性,只需转换它(在本例中为BtnRenderer ):

var _highlightBackgroundColor = (Element as BtnRenderer).HighlightedBackgroundColor.ToUIColor();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM