ListView 项目上的 Xamarin Forms AutomationId

Xamarin Forms AutomationId on ListView items

我正在尝试在 Xamarin Forms 中使用可绑定的 AutomationId 属性 以更好地与 Tosca Automation 配合使用。我有一个 ListView,假设我用类型为 "Test" 的对象列表填充它,称为 TestList。我基本上想将 AutomationId 设置为 "Test-{TestList.indexOf(testObject)}"。

我试过将它绑定到递增的 ID,但如果一个屏幕上有两个 ListView,那也无济于事。没有办法从另一个列表中唯一地识别一个列表。我需要对象类型 + 唯一 ID。

如果我用 3 个 "Test" 对象填充列表,最终结果会将 ContentDescriptions 设置为:Test0、Test1、Test2

有人知道是否有 "industry standard" 或简单、可维护的方法吗?

这只是一个例子。代码可能无法正常工作,因为我是在 SO 编辑器中输入的,而不是在任何代码编辑器中输入的。希望你明白:

型号:

public class TestObject
{
    public string Id {get;set;}
    public string DisplayText {get;set;}
}

ViewModel:

public class TestPageViewModel
{
     public ObservableList<TestObject> TestObjects= new ObservableList<TestObject>();

     //Skipped all Data initialization logics.
}

Xaml 第一个列表:

<ListView x:Name="List1" AutomationId="List1" ItemSource="{Binding TestObjects">
    <ListView.ItemTemplate>
         <DataTemplate>
              <ViewCell>
                   <StackLayout Orientation="Horizontal" AutomationId="{Binding Id,StringFormat='$List1Test{0}'}">
                        <!-- Rest of your Xaml -->
                   </StackLayout>
              <ViewCell>
         <DataTemplate>
    <ListView.ItemTemplate>          
</ListView>

第二个列表:

<ListView x:Name="List2" AutomationId="List2" ItemSource="{Binding TestObjects">
    <ListView.ItemTemplate>
         <DataTemplate>
              <ViewCell>
                   <StackLayout Orientation="Horizontal" AutomationId="{Binding Id,StringFormat='$List2Test{0}'}">
                        <!-- Rest of your Xaml -->
                   </StackLayout>
              <ViewCell>
         <DataTemplate>
    <ListView.ItemTemplate>          
</ListView>