ArticleDirectory
- Create a column-based Metro app
Http://blog.csdn.net/zhangxin09/article/details/6793593
Read by 285 Comment (0) Favorites Report
Translated by Chris sells: sp42
This is your first Metro application built with JavaScript.ProgramThe last part of the series will show you how to use 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.
Implementation 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:
View plain
-
- <Body>
-
- <H1>The old new thing</H1>
- <Div ID="Downloadstatus"></Div>
-
- <Div ID="Template" Data-win-control="Winjs. Binding. template">
- Div data-win-bind = "innertext: title " class = " posttitle " Div
- <Div Data-win-bind="Innertext: Date" Class="Postdate"></Div>
-
- </Div>
- <Div ID="Posts" Data-win-control="Winjs. UI. listview"
-
- Data-win-Options= "{Itemrenderer: Template, layout: {type: winjs. UI. listlayout },
-
- Selectionmode: 'Single ', onselectionchanged: selectionchanged }">
- </Div>
-
- <Div ID="Contenttemplate" Data-win-control="Winjs. Binding. template">
- Div data-win-bind = "innertext: title " class = " posttitle " Div
- Div data-win-bind = "innertext: date " class = " postdate " Div
- <Div Data-win-bind="Innerhtml: content" Class="Postcontent"></Div>
-
- </Div>
- <Div ID="Content"></Div>
-
- </Body>
Based on the record selected by the user in 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:
View plain
-
- FunctionSelectionchanged (e ){
- Content. innerhtml ="";
-
- VaRSelection = posts. wincontrol. selection;
-
-
- If(Selection. Length ){
-
- VaRPost = postitems [Selection [0]. Begin];
- VaRContenttemplate = winjs. UI. getcontrol (document. getelementbyid ("Contenttemplate"));
-
- Contenttemplate. Render (post). Then (Function(Element ){
-
- Content. appendchild (element );
-
- });
-
- }
-
- }
-
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.
View plain
-
- Body {
-
- Background-color:# Fff;
-
- Color:#000;
-
- Font-family:Verdana;
- Padding:8pt;
-
- Display:-MS-grid;
-
- -MS-grid-rows:Auto 1Fr;
-
- -MS-grid-columns:1FR1Fr;
-
- }
-
-
- ...
-
-
- # Posts{
- Width:99%;
-
- Height:100%;
-
- Overflow:Auto;
-
- -MS-grid-row:2;
-
- -MS-grid-column:1;
-
- }
-
-
- ...
-
- # ContenT {
-
- Width:95%;
-
- Height:100%;
-
- Overflow-Y:Auto;
-
- Margin-Right:64px;
-
- -MS-grid-row:2;
-
- -MS-grid-column:2;
- }
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 line is used to place titles without changes (if we want to handle overflow, we can also span two columns of span 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 reflected on the right, just as we do in 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.
- more resources of the metro app can be found in the resource center. If you encounter problems, you can also use related resources to solve the problems.