diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Android/VisualElementRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/VisualElementRenderer.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs b/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs index 19b05e2e..46bab02e 100644 --- a/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs +++ b/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs @@ -23,6 +23,10 @@ namespace Xamarin.Forms.Platform.Android VisualElementRendererFlags _flags = VisualElementRendererFlags.AutoPackage | VisualElementRendererFlags.AutoTrack; + string _defaultContentDescription; + bool? _defaultFocusable; + string _defaultHint; + int? _defaultLabelFor; InnerGestureListener _gestureListener; VisualElementPackager _packager; PropertyChangedEventHandler _propertyChangeHandler; @@ -209,6 +213,9 @@ namespace Xamarin.Forms.Platform.Android if (element != null && !string.IsNullOrEmpty(element.AutomationId)) SetAutomationId(element.AutomationId); + SetContentDescription(); + SetFocusable(); + Performance.Stop(); } @@ -302,6 +309,12 @@ namespace Xamarin.Forms.Platform.Android UpdateBackgroundColor(); else if (e.PropertyName == VisualElement.InputTransparentProperty.PropertyName) InputTransparent = Element.InputTransparent; + else if (e.PropertyName == Accessibility.HintProperty.PropertyName) + SetContentDescription(); + else if (e.PropertyName == Accessibility.NameProperty.PropertyName) + SetContentDescription(); + else if (e.PropertyName == Accessibility.IsInAccessibleTreeProperty.PropertyName) + SetFocusable(); } protected override void OnLayout(bool changed, int l, int t, int r, int b) @@ -331,6 +344,62 @@ namespace Xamarin.Forms.Platform.Android ContentDescription = id; } + protected virtual void SetContentDescription() + { + if (Element == null) + return; + + if (SetHint()) + return; + + if (_defaultContentDescription == null) + _defaultContentDescription = ContentDescription; + + var elemValue = string.Join(" ", (string)Element.GetValue(Accessibility.NameProperty), (string)Element.GetValue(Accessibility.HintProperty)); + + if (!string.IsNullOrWhiteSpace(elemValue)) + ContentDescription = elemValue; + else + ContentDescription = _defaultContentDescription; + } + + protected virtual void SetFocusable() + { + if (Element == null) + return; + + if (!_defaultFocusable.HasValue) + _defaultFocusable = Focusable; + + Focusable = (bool)((bool?)Element.GetValue(Accessibility.IsInAccessibleTreeProperty) ?? _defaultFocusable); + } + + protected virtual bool SetHint() + { + if (Element == null) + return false; + + var textView = this as global::Android.Widget.TextView; + if (textView == null) + return false; + + // Let the specified Title/Placeholder take precedence, but don't set the ContentDescription (won't work anyway) + if (((Element as Picker)?.Title ?? (Element as Entry)?.Placeholder ?? (Element as EntryCell)?.Placeholder) != null) + return true; + + if (_defaultHint == null) + _defaultHint = textView.Hint; + + var elemValue = string.Join(". ", (string)Element.GetValue(Accessibility.NameProperty), (string)Element.GetValue(Accessibility.HintProperty)); + + if (!string.IsNullOrWhiteSpace(elemValue)) + textView.Hint = elemValue; + else + textView.Hint = _defaultHint; + + return true; + } + protected void SetPackager(VisualElementPackager packager) { _packager = packager; @@ -357,6 +426,14 @@ namespace Xamarin.Forms.Platform.Android UpdateGestureRecognizers(); } + void IVisualElementRenderer.SetLabelFor(int? id) + { + if (_defaultLabelFor == null) + _defaultLabelFor = LabelFor; + + LabelFor = (int)(id ?? _defaultLabelFor); + } + void SubscribeGestureRecognizers(VisualElement element) { var view = element as View; |