如何解决在 Xamarin.Forms.UWP 中,如果未使用 SetNativeControl() 方法,则无法将自动化 ID 设置为自定义控件
我正在 Xamarin.Forms 中创建一个自定义控件,它是从 TemplatedView 继承的,并在所有三个平台上使用该应用程序。出于测试目的,我需要为所有原生控件设置自动化 ID。
通常,通过使用 AutomationPeer,我们可以将自动化 ID 设置为来自 UWP 渲染器项目的本机 UWP 控件。但是,在我的自定义控件中,我得到了这个。控件始终为空。
因此,我无法在 UWP 渲染器类中设置自动化 ID,因为 Forms TemplatedView 是 UWP 中的 FrameworkElement。
我的查询是,
-
如何在 Xamarin.Forms TemplatedView 的 UWP 渲染器项目中获取本机元素 (this.Control)。
-
如果 this.Control 为 null,如何将自动化 Id 设置为原生 UWP 控件。
代码片段如下:
class FormsCustomLayout : TemplatedView
{
public static readonly BindableProperty InputViewProperty =
BindableProperty.Create(nameof(InputView),typeof(View),typeof(FormsCustomLayout),null,BindingMode.Default,OnInputViewChanged);
private static void OnInputViewChanged(BindableObject bindable,object oldValue,object newValue)
{
(bindable as FormsCustomLayout).OnInputViewChanged(oldValue,newValue);
}
private void OnInputViewChanged(object oldValue,object newValue)
{
var oldView = (View)oldValue;
if (oldView != null)
{
if (this.contentGrid.Children.Contains(oldView))
{
oldView.SizeChanged -= OnInputViewSizeChanged;
oldView.BindingContext = null;
this.contentGrid.Children.Remove(oldView);
}
}
var newView = (View)newValue;
if (newView != null)
{
newView.SizeChanged += OnInputViewSizeChanged;
newView.VerticalOptions = Layoutoptions.CenterandExpand;
newView.HeightRequest = 60;
this.contentGrid.Children.Add(newView);
}
}
private void OnInputViewSizeChanged(object sender,EventArgs e)
{
}
public View InputView
{
get { return (View)GetValue(InputViewProperty); }
set { SetValue(InputViewProperty,value); }
}
internal void UpdateText(object text)
{
this.Text = (string)text;
}
private readonly Grid contentGrid = new Grid();
public FormsCustomLayout()
{
this.ControlTemplate = new ControlTemplate(typeof(StackLayout));
((StackLayout)Children[0]).Children.Add(this.contentGrid);
if (InputView != null)
{
this.contentGrid.Children.Add(InputView);
}
}
internal string Text { get; private set; }
}
class FormsCustomLayoutRenderer : VieWrenderer<FormsCustomLayout,FrameworkElement>
{
/// <summary>
/// Method that is called when the automation id is set.
/// </summary>
/// <param name="id">The automation id.</param>
protected override void SetAutomationId(string id)
{
if (this.Control == null)
{
base.SetAutomationId(id);
}
else
{
this.SetAutomationPropertiesAutomationId(id);
this.Control.SetAutomationPropertiesAutomationId(id);
}
}
/// <summary>
/// Provide automation peer for the control
/// </summary>
/// <returns>The TextInputLayout view automation peer.</returns>
protected override AutomationPeer OnCreateAutomationPeer()
{
if (this.Control == null)
{
return new FormsCustomLayoutAutomationPeer(this);
}
return new FormsCustomLayoutAutomationPeer(this.Control);
}
protected override void OnElementChanged(ElementChangedEventArgs<FormsCustomLayout> e)
{
base.OnElementChanged(e);
var element = e.NewElement;
}
}
internal class FormsCustomLayoutAutomationPeer : FrameworkElementAutomationPeer
{
/// <summary>
/// Initializes a new instance of the <see cref="FormsCustomLayoutRenderer"/> class.
/// </summary>
/// <param name="owner">FormsCustomLayout View control</param>
public FormsCustomLayoutAutomationPeer(FrameworkElement owner) : base(owner)
{
}
/// <summary>
/// Describe the control type
/// </summary>
/// <returns>The control type.</returns>
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
/// <summary>
/// Describe the class name.
/// </summary>
/// <returns>The Class Name.</returns>
protected override string GetClassNameCore()
{
return "FormsCustomLayout";
}
}
此处附上演示示例:FormsCustomControl
问候,
Bharathiraja。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。