2016-03-22 23:02:25 +03:00
|
|
|
|
using Windows.UI.Xaml;
|
2017-01-31 22:49:15 +03:00
|
|
|
|
using Windows.UI.Xaml.Automation.Peers;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Platform.UWP
|
|
|
|
|
{
|
|
|
|
|
public class ViewRenderer<TElement, TNativeElement> : VisualElementRenderer<TElement, TNativeElement> where TElement : View where TNativeElement : FrameworkElement
|
|
|
|
|
{
|
2017-01-31 22:49:15 +03:00
|
|
|
|
string _defaultAutomationPropertiesName;
|
|
|
|
|
AccessibilityView? _defaultAutomationPropertiesAccessibilityView;
|
|
|
|
|
string _defaultAutomationPropertiesHelpText;
|
|
|
|
|
UIElement _defaultAutomationPropertiesLabeledBy;
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
protected override void OnElementChanged(ElementChangedEventArgs<TElement> e)
|
|
|
|
|
{
|
|
|
|
|
base.OnElementChanged(e);
|
|
|
|
|
|
|
|
|
|
if (e.NewElement != null)
|
|
|
|
|
{
|
|
|
|
|
UpdateBackgroundColor();
|
2017-11-09 17:31:15 +03:00
|
|
|
|
UpdateFlowDirection();
|
2018-05-03 20:27:32 +03:00
|
|
|
|
UpdateMargins();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetAutomationId(string id)
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
{
|
|
|
|
|
base.SetAutomationId(id);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-05-10 20:20:57 +03:00
|
|
|
|
SetValue(Windows.UI.Xaml.Automation.AutomationProperties.AutomationIdProperty, $"{id}_Container");
|
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.AutomationIdProperty, id);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-31 22:49:15 +03:00
|
|
|
|
protected override void SetAutomationPropertiesName()
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
{
|
|
|
|
|
base.SetAutomationPropertiesName();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Element == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (_defaultAutomationPropertiesName == null)
|
2017-05-10 20:20:57 +03:00
|
|
|
|
_defaultAutomationPropertiesName = (string)Control.GetValue(Windows.UI.Xaml.Automation.AutomationProperties.NameProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
|
2017-05-10 20:20:57 +03:00
|
|
|
|
var elemValue = (string)Element.GetValue(AutomationProperties.NameProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(elemValue))
|
2017-05-10 20:20:57 +03:00
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.NameProperty, elemValue);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
else
|
2017-05-10 20:20:57 +03:00
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.NameProperty, _defaultAutomationPropertiesName);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetAutomationPropertiesAccessibilityView()
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
{
|
|
|
|
|
base.SetAutomationPropertiesAccessibilityView();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Element == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!_defaultAutomationPropertiesAccessibilityView.HasValue)
|
2017-05-10 20:20:57 +03:00
|
|
|
|
_defaultAutomationPropertiesAccessibilityView = (AccessibilityView)Control.GetValue(Windows.UI.Xaml.Automation.AutomationProperties.AccessibilityViewProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
|
|
|
|
|
var newValue = _defaultAutomationPropertiesAccessibilityView;
|
2017-05-10 20:20:57 +03:00
|
|
|
|
var elemValue = (bool?)Element.GetValue(AutomationProperties.IsInAccessibleTreeProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
|
|
|
|
|
if (elemValue == true)
|
|
|
|
|
newValue = AccessibilityView.Content;
|
|
|
|
|
else if (elemValue == false)
|
|
|
|
|
newValue = AccessibilityView.Raw;
|
|
|
|
|
|
2017-05-10 20:20:57 +03:00
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.AccessibilityViewProperty, newValue);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetAutomationPropertiesHelpText()
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
{
|
|
|
|
|
base.SetAutomationPropertiesHelpText();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Element == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (_defaultAutomationPropertiesHelpText == null)
|
2017-05-10 20:20:57 +03:00
|
|
|
|
_defaultAutomationPropertiesHelpText = (string)Control.GetValue(Windows.UI.Xaml.Automation.AutomationProperties.HelpTextProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
|
2017-05-10 20:20:57 +03:00
|
|
|
|
var elemValue = (string)Element.GetValue(AutomationProperties.HelpTextProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(elemValue))
|
2017-05-10 20:20:57 +03:00
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.HelpTextProperty, elemValue);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
else
|
2017-05-10 20:20:57 +03:00
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.HelpTextProperty, _defaultAutomationPropertiesHelpText);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetAutomationPropertiesLabeledBy()
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
{
|
|
|
|
|
base.SetAutomationPropertiesLabeledBy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Element == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (_defaultAutomationPropertiesLabeledBy == null)
|
2017-05-10 20:20:57 +03:00
|
|
|
|
_defaultAutomationPropertiesLabeledBy = (UIElement)Control.GetValue(Windows.UI.Xaml.Automation.AutomationProperties.LabeledByProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
|
2017-05-10 20:20:57 +03:00
|
|
|
|
var elemValue = (VisualElement)Element.GetValue(AutomationProperties.LabeledByProperty);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
var renderer = elemValue?.GetOrCreateRenderer();
|
|
|
|
|
var nativeElement = renderer?.GetNativeElement();
|
|
|
|
|
|
|
|
|
|
if (nativeElement != null)
|
2017-05-10 20:20:57 +03:00
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.LabeledByProperty, nativeElement);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
else
|
2017-05-10 20:20:57 +03:00
|
|
|
|
Control.SetValue(Windows.UI.Xaml.Automation.AutomationProperties.LabeledByProperty, _defaultAutomationPropertiesLabeledBy);
|
2017-01-31 22:49:15 +03:00
|
|
|
|
}
|
2017-11-09 17:31:15 +03:00
|
|
|
|
|
|
|
|
|
void UpdateFlowDirection()
|
|
|
|
|
{
|
|
|
|
|
Control.UpdateFlowDirection(Element);
|
|
|
|
|
}
|
2018-05-03 20:27:32 +03:00
|
|
|
|
|
|
|
|
|
void UpdateMargins()
|
|
|
|
|
{
|
|
|
|
|
Margin = new Windows.UI.Xaml.Thickness(Element.Margin.Left, Element.Margin.Top, Element.Margin.Right, Element.Margin.Bottom);
|
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|