2
votes

I'm trying to use the AutomationId bindable property in Xamarin Forms to work better with Tosca Automation. I have a ListView, and lets say I populate it with a list of objects of type "Test", called TestList. I would essentially like to set the AutomationId to "Test-{TestList.indexOf(testObject)}".

I've tried binding it to an incremented ID, but that doesn't help if there are two ListViews on one screen. There would be no way to uniquely identify one list from another. I need to have the object type + unique ID.

If I populate the list with 3 "Test" objects, the end result would set the ContentDescriptions to: Test0, Test1, Test2

Does anyone know if there is an "industry standard" or easy, maintainable way of doing this?

1
If there are two listview on same page then why don't you simply put naming convention! Like "List1Test-1", "List2Test-1"? Everything else you said that you are already using the bindable index. I think that should be all good. - Nirmal Subedi
@NirmalSubedi Sorry I might have worded it poorly, I don't actually understand how to set the AutomationId to "Test0". Do you know how to set that value to index, prefixed by the object type? - jakelli
Can you post the Model that you are using? Are there any data related ID's in Test Object? Or you just want to use the Index? - Nirmal Subedi

1 Answers

2
votes

This is just an example. Code might not work properly because I typed in SO Editor not in any Code editor. Hope you understand:

Model:

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 First List:

<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>

Second List:

<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>