[Win8]Windows8開發筆記(三):代碼讀取圖片動態產生頁面

來源:互聯網
上載者:User

建立一個項目,叫做LianliankanTest。

建立一個檔案夾Images用來儲存圖片,我們拖動10個圖片進入到檔案夾中:



開啟MainPage.xaml.cs,找到重寫的OnNavigatedTo方法。這個方法實在跳轉到該頁面的時候調用,所以我們把頁面的動態初始化加在這裡。

前面說到,Grid布局中主要是RowDefinition和ColumnDefinition來產生行列,我們先在MainPage.xaml中定義首頁面的Grid的name為homepage,以便後台調用。

然後在MainPage.xaml.cs,找到重寫的OnNavigatedTo方法,寫上動態添加的行列定義:

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 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";                    //指定圖片元素                    img.Source = new BitmapImage(new Uri(fileName));                    //為其指定某行某列                    homepage.Children.Add(img);                    Grid.SetRow(img, row);                    Grid.SetColumn(img, col);                }            }        }

其中,ms-appx:///是為了指定當前路徑。如果位於項目中,需要指定ms-appx:///。

運行一下就能看到 頁面的效果:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.