简体   繁体   中英

How to access BindableProperty in PCL renderer from iOS customrenderer

Im trying to access my bindable property for a custom button that I attempt to code a renderer for. First here is my PCL renderer:

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);
        }
    }
}

As you can see, I intend to set a HighlightedBackgroundColor from XAML, However, I don't know how to access it in my iOS renderer, what I have is:

[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;
                });
            };
        }
    }
}

How do I access this property correctly?

//HERE IS MY PROBLEM

var _highlightBackgroundColor = Element.HighlightedBackgroundColor.ToUIColor();

Directly using Element is the base of your renderer ( VisualElementRenderer<TElement> ) so in order to access any custom properties on your subclass, just cast it ( BtnRenderer in this case):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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