diff options
Diffstat (limited to 'Xamarin.Forms.Platform.WinRT/VisualElementRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.WinRT/VisualElementRenderer.cs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.WinRT/VisualElementRenderer.cs b/Xamarin.Forms.Platform.WinRT/VisualElementRenderer.cs index b81f73e3..402d1da8 100644 --- a/Xamarin.Forms.Platform.WinRT/VisualElementRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT/VisualElementRenderer.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using Windows.Foundation; using Windows.UI.Xaml; using Windows.UI.Xaml.Automation; +using Windows.UI.Xaml.Automation.Peers; using Windows.UI.Xaml.Controls; #if WINDOWS_UWP @@ -17,6 +18,10 @@ namespace Xamarin.Forms.Platform.WinRT public class VisualElementRenderer<TElement, TNativeElement> : Panel, IVisualElementRenderer, IDisposable, IEffectControlProvider where TElement : VisualElement where TNativeElement : FrameworkElement { + string _defaultAutomationPropertiesName; + AccessibilityView? _defaultAutomationPropertiesAccessibilityView; + string _defaultAutomationPropertiesHelpText; + UIElement _defaultAutomationPropertiesLabeledBy; bool _disposed; EventHandler<VisualElementChangedEventArgs> _elementChangedHandlers; VisualElementTracker<TElement, TNativeElement> _tracker; @@ -108,6 +113,11 @@ namespace Xamarin.Forms.Platform.WinRT return new SizeRequest(result); } + public UIElement GetNativeElement() + { + return Control; + } + public void SetElement(VisualElement element) { TElement oldElement = Element; @@ -287,6 +297,14 @@ namespace Xamarin.Forms.Platform.WinRT UpdateEnabled(); else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName) UpdateBackgroundColor(); + else if (e.PropertyName == Accessibility.HintProperty.PropertyName) + SetAutomationPropertiesHelpText(); + else if (e.PropertyName == Accessibility.NameProperty.PropertyName) + SetAutomationPropertiesName(); + else if (e.PropertyName == Accessibility.IsInAccessibleTreeProperty.PropertyName) + SetAutomationPropertiesAccessibilityView(); + else if (e.PropertyName == Accessibility.LabeledByProperty.PropertyName) + SetAutomationPropertiesLabeledBy(); } protected virtual void OnRegisterEffect(PlatformEffect effect) @@ -300,6 +318,75 @@ namespace Xamarin.Forms.Platform.WinRT SetValue(AutomationProperties.AutomationIdProperty, id); } + protected virtual void SetAutomationPropertiesName() + { + if (Element == null || Control == null) + return; + + if (_defaultAutomationPropertiesName == null) + _defaultAutomationPropertiesName = (string)Control.GetValue(AutomationProperties.NameProperty); + + var elemValue = (string)Element.GetValue(Accessibility.NameProperty); + + if (!string.IsNullOrWhiteSpace(elemValue)) + Control.SetValue(AutomationProperties.NameProperty, elemValue); + else + Control.SetValue(AutomationProperties.NameProperty, _defaultAutomationPropertiesName); + } + + protected virtual void SetAutomationPropertiesAccessibilityView() + { + if (Element == null || Control == null) + return; + + if (!_defaultAutomationPropertiesAccessibilityView.HasValue) + _defaultAutomationPropertiesAccessibilityView = (AccessibilityView)Control.GetValue(AutomationProperties.AccessibilityViewProperty); + + var newValue = _defaultAutomationPropertiesAccessibilityView; + var elemValue = (bool?)Element.GetValue(Accessibility.IsInAccessibleTreeProperty); + + if (elemValue == true) + newValue = AccessibilityView.Content; + else if (elemValue == false) + newValue = AccessibilityView.Raw; + + Control.SetValue(AutomationProperties.AccessibilityViewProperty, newValue); + } + + protected virtual void SetAutomationPropertiesHelpText() + { + if (Element == null || Control == null) + return; + + if (_defaultAutomationPropertiesHelpText == null) + _defaultAutomationPropertiesHelpText = (string)Control.GetValue(AutomationProperties.HelpTextProperty); + + var elemValue = (string)Element.GetValue(Accessibility.HintProperty); + + if (!string.IsNullOrWhiteSpace(elemValue)) + Control.SetValue(AutomationProperties.HelpTextProperty, elemValue); + else + Control.SetValue(AutomationProperties.HelpTextProperty, _defaultAutomationPropertiesHelpText); + } + + protected virtual void SetAutomationPropertiesLabeledBy() + { + if (Element == null || Control == null) + return; + + if (_defaultAutomationPropertiesLabeledBy == null) + _defaultAutomationPropertiesLabeledBy = (UIElement)Control.GetValue(AutomationProperties.LabeledByProperty); + + var elemValue = (VisualElement)Element.GetValue(Accessibility.LabeledByProperty); + var renderer = elemValue?.GetOrCreateRenderer(); + var nativeElement = renderer?.GetNativeElement(); + + if (nativeElement != null) + Control.SetValue(AutomationProperties.LabeledByProperty, nativeElement); + else + Control.SetValue(AutomationProperties.LabeledByProperty, _defaultAutomationPropertiesLabeledBy); + } + protected void SetNativeControl(TNativeElement control) { TNativeElement oldControl = Control; @@ -367,6 +454,10 @@ namespace Xamarin.Forms.Platform.WinRT protected virtual void UpdateNativeControl() { UpdateEnabled(); + SetAutomationPropertiesHelpText(); + SetAutomationPropertiesName(); + SetAutomationPropertiesAccessibilityView(); + SetAutomationPropertiesLabeledBy(); } internal virtual void OnElementFocusChangeRequested(object sender, VisualElement.FocusRequestArgs args) |