Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

iOS Renderer using the RightView property of UITextField

$
0
0

I am trying to create a custom renderer for a Entry subclass so that I can show a tick or cross when validation passes or fails.

The approach I have taken is as follows:

  1. Created a behavior that attaches to the TextChanged event and performs a validation.
  2. This is turn sets a custom bindable property on Entry subclass called IsValid with a type of bool
  3. The custom renderer is listening for changes on the IsValid property and in turn setting the rightView property of the Control

The problem I am having is that the tick or cross is drawn the first time but any subsequent changes to the IsValid property is not reflected in the rightView.

Here is a snippet of the renderer code:

protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged (e);
    if (Element != null) {
        if (Control.RightView ==null) {
            CreateRightView ();
        }
    }
}

protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged (sender, e);
    if (string.IsNullOrEmpty (e.PropertyName) || e.PropertyName == "IsValid")
        SetRightView (Element);
}

void SetRightView (PfgEntry view)
{
    Debug.WriteLine ("Setting Right View Valid State: {0}", view.IsValid);
    var textToShow = "";
    textToShow = view.IsValid ? Icons.Valid : Icons.Invalid;
    _validDisplayLabel.TextColor = view.IsValid ? UIColor.Green : UIColor.Red;
    _validDisplayLabel.Text = textToShow;
    _validDisplayLabel.TextAlignment = UITextAlignment.Center;
    _validDisplayLabel.SizeToFit ();
    _validDisplayLabel.Center = _rightView.Center;
    _rightView.AddSubview (_validDisplayLabel);
}

void CreateRightView()
{
    _rightView = _rightView ?? new UIView (new RectangleF (0, 0, 29, 29));
    _validDisplayLabel = _validDisplayLabel ?? new UILabel (new RectangleF (0, 0, 29, 29));
    _rightView.AddSubview (_validDisplayLabel);
    Control.RightView = _rightView;
    Control.RightViewMode = UITextFieldViewMode.Always; 
    SetRightView ((PfgEntry)Element);
}

I know that the process is been followed as it should be as I have followed Debug.WriteLine responses throughout the process.

Hopefully somebody can shed some light.

Thanks


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>