Xamarin Forms AutomationId on ListView items



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

我尝试将其绑定到递增的 ID,但如果一个屏幕上有两个 ListViews,这无济于事。没有办法唯一地将一个列表与另一个列表区分开来。我需要有对象类型 + 唯一 ID。

如果我用 3 个"测试"对象填充列表,最终结果会将内容描述设置为:Test0、Test1、Test2

有谁知道是否有"行业标准"或简单、可维护的方法?

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

型:

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

视图模型:

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>

最新更新