Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting resource names of Elements in Xamarin Forms

I would like to be able to reference the elements created after compiling a Xamarin Forms application into Android.


Example Code:

<Entry Placeholder="Username" 
       Text="{Binding UserName, Mode=TwoWay}"
       x:Name="UsernameEntry"
       x:Key="UsernameEntry" />

<Entry Placeholder="Password"
       IsPassword="True" 
       Text="{Binding Password, Mode=TwoWay}" 
       x:Name="PasswordEntry" 
       x:Key="PasswordEntry" />

<Button Text="{extensions:Translate LogIn}" 
        Command="{Binding LoginUserCommand}"
        x:Name="LoginButton" 
        x:Key="LoginButton" />

From what I can see what is generated in the way of Elements is as so:

android:id​/content​/RelativeLayout[0]​/PlatformRenderer[0]​/NavigationPageRenderer[0]​/PageContainer[0]​/PageRenderer[0]​/Platform_DefaultRenderer[0]​/Platform_DefaultRenderer[1]​/EntryRenderer[0]​/FormsEditText[0]
android:id​/content​/RelativeLayout[0]​/PlatformRenderer[0]​/NavigationPageRenderer[0]​/PageContainer[0]​/PageRenderer[0]​/Platform_DefaultRenderer[0]​/Platform_DefaultRenderer[1]​/EntryRenderer[1]​/FormsEditText[0]
android:id​/content​/RelativeLayout[0]​/PlatformRenderer[0]​/NavigationPageRenderer[0]​/PageContainer[0]​/PageRenderer[0]​/Platform_DefaultRenderer[0]​/Platform_DefaultRenderer[1]​/ButtonRenderer[2]​/AppCompatButton[0]

Which is not usable, I just want to be able to name the resources as so:

UsernameEntry
PasswordEntry
LoginButton

and then reference them.


Use Cases:

  • Firebase Testing:

enter image description here

  • Play Store Pre-Launch Reports:

enter image description here

I feel like this should be easy to do but I have spent a good part of a day looking and found nothing any help would be much appreciated.

like image 834
Jesse Whitham Avatar asked Oct 16 '22 10:10

Jesse Whitham


1 Answers

Those Android widgets/controls are dynamically created (via Forms' XAML in this case) and not from AXML layouts being inflated thus there are no layouts to be parsed and no resource names assigned (no R.* constants assigned).

The pre-launch Sign-in credentials require a resource name and not an ID. FYI: Widget IDs can be assigned at runtime (i.e. via a custom Forms' renderer for that control), but that does not help in this case as you need the string-based Resource name.

If your Forms' app requires demo/testing credentials and you what to make use of the Play Store pre-reports, you will have to pass on using the automated test crawler and supply a custom Robo script to be run for your tests.

Note:

You could create a dummy layout in your Xamarin.Android project with some made-up resource Names and thus at build-time those names are assigned int IDs in the auto-generated C# Resource.Id and Java R.ID files, and then at runtime (in native code, via DI/IoC/Forms' Renderer/...) obtain the Android Widgets for that Forms-based login page and assign the ID property of those username/password Widgets using the IDs that were created from the dummy layout's resource string names.

like image 96
SushiHangover Avatar answered Oct 20 '22 16:10

SushiHangover