We have seen examples earlier where we need to generate a dynamic form UI, from a JSON, that we read via WEB API using API calls, the same technique can be applied to generate a ListView.
Why do we need a Dynamic ListView?
There are times when we cannot completely know what kind of data we may acquire using API calls, we may have a limited idea of the overall model and the UI, where there are some definite Names such as Alias, Name and there might be some Boolean fields this data is stored in a UI JSON. But we never know the value that are stored in those Name/Value pairs
The key steps of this technique are:
1) We will read and deserialize the JSON.
2) Create the Models for the data and the UI.
3) Create the DataTemplate in C#.
4) Add the DataTemplate to the ListView.
1) Read the JSON
This is how we read a JSON using filing handling, we took this route just to bring the API into the project and for demonstrating our main functionality, usually we will use a Web API to get the JSON, here we store both our JSON in a string field and then deserialize it.
2) Create the Models for the Data and UI
We Add a new class file in our DynamicListUI project where we create the corresponding models for both the lists.
3) Create a DataTemplate using C#
4) Add the DataTemplate to the Listview.
This will add the data template to the listview.
After completing the final step the result will look like this:
Obviously, we can use frames and make this more elegant looking, but this is the way we actually generate a dynamic list, we can also added sort and search functionalities on this but, that will be demonstrated in another post.