Reusing Text views and Grid

后端 未结 1 1982
感动是毒
感动是毒 2021-01-26 08:16

I am working on windows phone 8 app.

I am dynamically creating multiple Textview and Grid inside For loop.

for (int j = 0; j < 300; j++)
                      


        
相关标签:
1条回答
  • 2021-01-26 08:28

    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.

    0 讨论(0)
提交回复
热议问题