'Passing Data with a list<T> property between Xamarin form and looping through that list to build the grid
I have a Xamarin app that on one page displays just the Header information of multiple reports. These reports have a standard fixed set of detail lines that could be displayed when selected, but the app does not know how many or which ones until the data is retrieved. When the Overview form is tapped the class is serialized (JsonConvert) to a string then deserialized in the ViewModel for the Details form. The details page is a XAML page but the content is built in C# because each of the detail lines will need to be inspected (foreach loop) to determine how the line should be constructed. The problem I have is that I do not know of a way to loop through the List once the object has been passed to the details form. The Model Class
public class TrackWarrant : Form
{
public string IssuedTo { get; set; }
public string AtLocation { get; set; }
public string AuthorityDirection { get; set; }
public string ProceedFrom { get; set; }
public string ProceedTo { get; set; }
public string FormDate { get; set; }
public string OKTime { get; set; }
public List<KeyValuePair<int, string>> TWLines { get; set; }
}
public class TWLines
{
public int FormNo;
public string LineNo;
}
How I serialize and pass the data from the Header page to the detail page
async void TrackWarrant_Tapped(object sender, EventArgs e)
{
var ea = e as TappedEventArgs;
if (ea.Parameter != null)
{
var forminfo = JsonConvert.SerializeObject(ea.Parameter);
await Shell.Current.GoToAsync($"trackwarrantinfo?Content={forminfo}");
}
This is what my ViewModel does with the passed through data
[QueryProperty(nameof(Content), nameof(Content))]
internal class TrackWarrantViewModel :BaseViewModel
{
string content = "";
public string Content
{
get => content;
set
{
content = Uri.UnescapeDataString(value ?? string.Empty);
OnPropertyChanged();
PopulateForm(content);
}
}
private TrackWarrant trackwarrant { get; set; }
public TrackWarrant TrackWarrantInfo
{
get => trackwarrant;
set
{
trackwarrant = value;
OnPropertyChanged("TrackWarrantInfo");
}
}
private void PopulateForm(string getcont)
{
var content = JsonConvert.DeserializeObject<TrackWarrant>(getcont);
TrackWarrantInfo = content;
}
}
In my Detail Page Code Behind I can extract data & display it like so: TrackWarrant is my exposed class and the FormNumber is a property e.g. my model above.
var lblFormNo = new Label
{
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
FontAttributes = FontAttributes.Bold,
Margin = new Thickness(5, 0, 0, 0),
};
lblFormNo.SetBinding(Label.TextProperty, "TrackWarrantInfo.FormNumber");
I can even extract data from the list like so:
lblTWLine.SetBinding(Label.TextProperty, "TrackWarrantInfo.TWLines[0].Key");
But I don't know how many line there are and what I really want to do is is build lines in my Grid (the basis of my content) by looping through each line of the detail lines inspecting it and then display those details. I have been searching the net for 2 days and coming up empty. Thanks in advance.
Solution 1:[1]
TWLines is a List<KeyValuePair<int,string>> so you should be able to iterate over it using foreach
foreach(var line in TWLines)
{
var key = line.Key;
var value = line.Value;
}
Solution 2:[2]
I've stumbled over the solution. Originally I was sending my JSON string to the ViewModel of the form and I changed that to sending it directly to the form. Code that I showed that was in my viewModel, [QueryProperty(nameof(Content), nameof(Content))], successive code, I put in the CodeBehind of the form. As I'm building the contents of the form I can loop through the detail data and determine how to construct the grid for display.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jason |
| Solution 2 | Gary H |
