Use JavaScript to build your first Metro application (onwindows 8) (3)

Source: Internet
Author: User
Document directory
  • Create a column-based Metro app

This is the last tutorial in the series "Build your first Metro application with JavaScript, with the help of the metro style and split template provided by Microsoft Visual Studio 11 express for Windows Developer Preview, how to Use the CSS style to make your program more compatible with windows look & feel.

Preface

This article is the third article about using JavaScript to build your first Metro application (on windows8). If you have not read the first and second articles, read them immediately. If you are familiar with HTML5, css3, and JavaScript, it is best to understand them without affecting your understanding of the subject.

 

Implement css3 grid layout

 

Microsoft has already achieved a considerable degree of web standards: HTML5, ecmascript Fifth Edition of the language specification and css3. One of the Standards in css3 is css3grid layout (grid layout ). With this technology, you can easily adjust the rows and columns of a table on the HTML page. In this example, the listview is in this layout. Listview can fill in all spaces, but our current requirement is to make the listview occupy half of the screen, put it on the left, and the right is the content area. Click listview to display details in the content area.

 

 

As shown in, we want two rows of grid, one row for the title, and one row for the RSS data. The RSS data area is split into two columns, one is the listview on the left and the other is the content. Let's take a look at how to do this. We need to draw a place on the right and place the template:

[HTML]View plaincopy

  1. <Body>
  2. <H1> the old New thing
  3. <Div id = "downloadstatus"> </div>
  4. <Div id = "template" data-win-control = "winjs. Binding. template">
  5. <Div data-win-bind = "innertext: Title" class = "posttitle"> </div>
  6. <Div data-win-bind = "innertext: Date" class = "postdate"> </div>
  7. </Div>
  8. <Div id = "posts" data-win-control = "winjs. UI. listview"
  9. Data-win-Options = "{itemrenderer: Template, layout: {type: winjs. UI. listlayout },
  10. Selectionmode: 'Single ', onselectionchanged: selectionchanged} ">
  11. </Div>
  12. <Div id = "contenttemplate" data-win-control = "winjs. Binding. template">
  13. <Div data-win-bind = "innertext: Title" class = "posttitle"> </div>
  14. <Div data-win-bind = "innertext: Date" class = "postdate"> </div>
  15. <Div data-win-bind = "innerhtml: content" class = "postcontent"> </div>
  16. </Div>
  17. <Div id = "content"> </div>
  18. </Body>

Based on the record selected by the user on the left listview, the content is displayed in the right div, And the template is used to define the appearance. We also need to set listview to the "single select" selection mode (the default value is none ). Once the content of the selected area changes, the event handler will be triggered. The processor itself is relatively simple:

 

[JavaScript]View plaincopy

  1. Function selectionchanged (e ){
  2. Content. innerhtml = "";
  3. VaR selection = posts. wincontrol. selection;
  4. If (selection. Length ){
  5. VaR post = postitems [Selection [0]. Begin];
  6. VaR contenttemplate = winjs. UI. getcontrol (document. getelementbyid ("contenttemplate "));
  7. Contenttemplate. Render (post). Then (function (element ){
  8. Content. appendchild (element );
  9. });
  10. }
  11. }

 

When the user clicks the content of an item on the listview, this means that either the selected content or the content is canceled (reversed ). Either event triggers the event processor. Specifically, first clear the content on the current right side, and then check the selection area. If there is a selection area, we extract the post records from the RSS data, then fill in the record data in the content template, as we did before. With rendered HTML elements, you can simply put the HTML elements on our content Div.

In addition to style adjustment, grid layout has almost finished its work.

[CSS]View plaincopy

  1. Body {
  2. Background-color: # FFF;
  3. Color: #000;
  4. Font-family: verdana;
  5. Padding: 8pt;
  6. Display:-MS-grid;
  7. -MS-grid-rows: auto 1fr;
  8. -MS-grid-columns: 1fr 1fr;
  9. }
  10. ...
  11. # Posts {
  12. Width: 99%;
  13. Height: 100%;
  14. Overflow: auto;
  15. -MS-grid-row: 2;
  16. -MS-grid-column: 1;
  17. }
  18. ...
  19. # Content {
  20. Width: 95%;
  21. Height: 100%;
  22. Overflow-Y: auto;
  23. Margin-Right: 64px;
  24. -MS-grid-row: 2;
  25. -MS-grid-column: 2;
  26. }

 

In the body style, we define the entire page as the gridl ayout mode. "-MS-grid-rows: auto1fr;" is divided into two rows, the adaptive width of one row (the maximum width is the width), and the remaining space is occupied by the other row. "-MS-grid-columns: 1fr1fr;" is divided into two columns on the page, both columns are equal. By default, all content is in the first row and column (the rows and columns are counted from 1, not 0 ). In other words, because the first row is used to place titles without changes (if we want to handle overflow, we can also span two columns across two columns ), the Post's listview is moved to the second row and the first column; The content area is moved to the second row and the second column. So the entire grid layout can be filled up.

Create a column-based Metro app

So far, our RSS reader program has jumped onto the paper. In fact, this style is so common that it is summarized into a specific project template called"Split Application". If you create a new project of this type and execute it, you will see that we have created a more beautiful version, although there is no real data. When you click the project on the left, you will see the data on the right, as we did in our simple RSS reader program:

 

Next step
Congratulations! You have built your first metro program. In the end, we suggest:
  • Find some windows Developer Preview sample gallery sample programs
  • Explore the reference section document of the metro style app.
  • You can explore more resources of the metro app in the resource center. If you encounter problems, you can also use related resources to solve the problems.

Original article address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.