Basically, I've been tasked to work on a Xamarin.iOS app (predominantly for tablets only) that would display a long scrollable form that contains ~200 controls (buttons, switches, text fields, etc...).

Normally, I would create an instance of the control, assign values to it's properties and then add the control to the view to display but with 200 controls, it seems like a lot of redundancy and long classes.

How can I best architect this app to prevent redundancy or make the controls populate dynamically?


Instead of

UILabel label1 = new UILabel(); 
label1.Text = "label text"; 

UILabel label2 = new UILabel(); 
label2.Text = "label text";

//then do this like 200 times to show labels with different names 

Can I do something like this?

//in a foreach with amount of controls 

UILabel label = new UILabel(); 
label.Text = dynamicallyGeneratedText; 

But in general, what's the best way to architect an app with long, scrollable form that the user has to fill out? What are some ways I can make my code more dynamic and less hardcoded?


  • 1
    Maybe this will help. searchenginepeople.com/blog/150450955-how-many-form-fields.html – RubberDuck Apr 23 '17 at 23:14
  • 5
    Redesign the view... There's something named UX. – Laiv May 23 '17 at 21:07
  • We need more information, what kind of inputs are they? Are they related at all? Distinct? – TheCatWhisperer Jun 22 '17 at 14:52
  • 1
    one page with 200 differents controls on a tablet ? Unless if this a kind of list like Ewan's answer suggest, who would want to use something like this ? You'd better go by grouping them and having a navigation system between those groups. – Walfrat Sep 21 '17 at 8:24

If you really need this many elements, then you should probably make the interface data-driven instead of entirely in code. This means that instead of 200 instances of new UILabel() in your code, you would have an array or other data structure that contained the text/ID of each label, then you would just use a for loop to iterate over these creating a new instance for each one. So instead of 400 lines creating labels you would just have 4 lines something like:

for(label : labels) {
    UILabel uilabel = new UILabel(); 
    uilabel.Text = label.text;

Once your UI is data-driven then this also opens up the possibility of the data being loaded from a configuration-file or from a database, so you can change the UI without having to modify or recompile the code at all.

| improve this answer | |
  • I agree. FWIW, in an asp.net web project with such (really) long forms I had decided to build them dynamically based on data stored in (the project's) database. In other words, each form was described in the database and built on the fly. Nevertheless, the variation of form controls was very limited (only labels, textboxes, checkboxes and buttons). – dpant Apr 22 '18 at 19:07

ListView https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/

But, getting this to be performant has been a notorious bug bear for Xamarin. You would be well advised to thoroughly test for performance on multiple real devices before committing to be able to produce the design in Xamarin.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.