My team is working on a project where controls are being created dynamically. On the ContentPage there are X number of StackLayouts, each with an Entry control. We would like to implement a "Clear" button to clear the Entries and a "Save" button to save the values in the Entry fields. Though I thought this would be a simple task involving iterating through the Entry fields in the ContentPage (or the StackLayouts), this has proved otherwise. What is the best way to accomplish this? Is it even possible to iterate?
Since the controls are dynamic, we can't set x:Name or this might be a lot easier.
What we were hoping for was saving the Entry IDs in a list and then iterating through controls to find those IDs later, but iteration seems to be an issue.
I am beginning to feel like Xamarin.Forms is designed to make it impossible to do this when it seems like such a basic process. Any help on this would be very appreciated.
My recommendation is to create the controls dynamically and keep track of them in code. You can use a dictionary or whatever data structure makes sense to keep track of them. That's the better way. Finding controls by name or id by recursing through the view hierarchy is just going to be very slow and indirect.
Answers
If you create the controls dynamically then you can keep track of them yourself.
Is not possible with reflection?
We are using a method to do that right now and one of my developers just came up with an iteration method that may be working. I am wondering what the recommended practice is for managing these controls.
Ideally, there would be a Name attribute for these controls where we could set the name, then use something akin to "FindControl" to find it by that name. But unless you use XAML for control creation this doesn't seem possible.
Right now we are using a list of the control's Id and our own field Id to compare against the control Ids of all of the controls within the ContentPage. If it matches, we've found the right one.
It just feels like there should be a better way.
My recommendation is to create the controls dynamically and keep track of them in code. You can use a dictionary or whatever data structure makes sense to keep track of them. That's the better way. Finding controls by name or id by recursing through the view hierarchy is just going to be very slow and indirect.
And I am a damn idiot. My brain was not working properly. I just added a List object and added them to that. You'd think after 25 years or so of programming this would have come to me a lot sooner. I am not familiar with Xamarin so I was coming at this problem in a complete wrong way.
Thanks for you help.