Reusing Text views and Grid

社会主义新天地 提交于 2019-12-02 08:59:44

Basically, instead of creating complicated objects directly at page cs code you should create a user control. On you user control you preset the positions for you element, i.e. if you want it to have an Image and a couple of textblocks, you just place them there. That would be a template.

Then, you create list of user controls in a loop.

So, your code would look like:

for (int j = 0; j < 300; j++)
{
    SomeUserControl someUserControl = new SomeUserControl(constuctorValue1, constuctorValue2);
    ListOfSomeUserControls.Add(someUserControl);
    LayoutRoot.Children.Add(someUserControl);
}

Where ListOfSomeUserControls is a List defined on your page code.

Here, you can send some data to constructor, for example someUserControl will be your ImageUri or text value.

OR, you can create some properties at user control to set data there.

OR, you can create some methods at user control, like ChangeImageUri() or something like that.

To use this dynamically created user controls you should use ListOfSomeUserControls[Index]. You could also save this list somewhere for former usage.

That's the basic idea.


In the link, you've shared, the OP creates a list of links to images. Feel, the difference: he has 5 Image boxes at the page and a list of 300 links. So, when you start your app you have this:

Image1::listOfUris[1] Image2::listOfUris[2] Image3::listOfUris[3] Image4::listOfUris[4] Image5::listOfUris[5]

When you tap next button, you just displace this list by 1.

Image1::listOfUris[2] Image2::listOfUris[3] Image3::listOfUris[4] Image4::listOfUris[5] Image5::listOfUris[6]

That's all, just 5 images, not 300.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!