代码之家  ›  专栏  ›  技术社区  ›  Jeppe Christensen

如何从iOS customrenderer访问PCL渲染器中的BindableProperty

  •  1
  • Jeppe Christensen  · 技术社区  · 6 年前

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

    HighlightedBackgroundColor 但是,从XAML开始,我不知道如何在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;
                    });
                };
            }
        }
    }
    

    如何正确访问此属性?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Community Dai    4 年前

    //这是我的问题

    var\u highlightBackgroundColor=Element.HighlightedBackgroundColor.ToUIColor();

    Element 是渲染器的基础( VisualElementRenderer<TElement> BtnRenderer 在这种情况下):

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