Create a project called lianliankantest.
Create a folder named images to store images. Drag 10 images to the folder:
Open mainpage. XAML. CS and find the override onnavigatedto method. This method is called when you jump to the page, so we add dynamic page initialization here.
As mentioned above, rowdefinition and columndefinition are used to generate columns in the grid layout. In mainpage. XAML, we first define the grid name on the home page as homepage for background calls.
In mainpage. XAML. CS, find the override onnavigatedto method, and write the column definition that is dynamically added:
Protected override void onnavigatedto (navigationeventargs e) {for (INT I = 0; I <10; I ++) {rowdefinition ROW = new rowdefinition (); homepage. rowdefinitions. add (ROW) ;}for (INT I = 0; I <10; I ++) {columndefinition Col = new columndefinition (); homepage. columndefinitions. add (COL);} random = new random (); For (int row = 0; row <10; row ++) {for (INT Col = 0; col <10; Col ++) {image IMG = new image (); int num = random. next (1, 10); string filename = "MS-appx: // images/" + num + ". jpg "; // specifies the Image Element IMG. source = new bitmapimage (New uri (filename); // specify a row with a column named "Homepage. children. add (IMG); grid. setrow (IMG, row); grid. setcolumn (IMG, col );}}}
Here, MS-appx: // is used to specify the current path. If you are in a project, you must specify MS-appx :///.
Run the following command to view the page effect: