TL;DR: I want to:
- Save some c# object in every StackLayout
- Use information from this object to render another StackLayouts if user wants it.
Details:
I want to display all Html DOM Tree of Objects of some website as a list.
I'm using Html Agility Pack to manage downloaded html. It creates a List of HtmlNode objects that has some HtmlNodes as children, that has some HtmlNodes as children etc. (just like in HTML DOM).
This is what I currently have:
private StackLayout GenerateTreeOfHtmlObjects(HtmlNode htmlNode, int padding)
{
StackLayout stackLayout = new StackLayout();
stackLayout.Orientation = StackOrientation.Vertical;
foreach (var childNode in htmlNode.ChildNodes)
{
if (childNode.Name != "#text")
{
Button showMoreButton = new Button { Text = ">", HorizontalOptions = LayoutOptions.Start, };
stackLayout.Children.Add(
new StackLayout
{
Children = {
showMoreButton,
new Label { Text = childNode.Name, FontSize=20 },
new Label { Text = new string(childNode.InnerText.Trim().ToCharArray().Take(30).ToArray()), FontSize=15 },
new Button { Text = "Zaznacz" },
GenerateTreeOfHtmlObjects(childNode, padding+10) //RECURSION, CALL THIS FUNCTION AGAIN AS LONG AS THERE ARE SOME CHILDREN
},
Padding = new Thickness(padding, 0, 0, 10)
}
);
}
}
return stackLayout;
}
As you see, I'm creating StackLayout for every HtmlNode and add another StackLayouts for their children. It works more or less as I wanted, but the problem is it takes too much memory and takes very long to load.
So I want to load children of some HtmlNode only if user click on their parent. I will load only those HtmlNodes that are on the top of the tree and successively load their children if they were needed.
To do this, I need to store somewhere in every StackLayout some information about what to load if user click on it. Every currently loaded StackLayout has to have HtmlNode class assigned that will allow me to load children of HtmlNode (every HtmlNode has ChildNodes variable and I will take information from there)