Textblock XAML files and hidden files
In the design field, we can design in XAML. This design is WYSIWYG and very convenient. Because all the elements used in XAML are actually class files, for example, in the textblock class, all the features displayed by textblock In The XAML file are the attributes and methods of this class. Therefore, we can build visual elements in the hidden file. For example, we can achieve the following effects:
The code for some files in mainpage. XAML is as follows:
<! -- Contentpanel-place other content here -->
<Grid X: Name = "contentpanel" grid. Row = "1" margin = "12,0, 12,0">
<Textblock X: Name = "txtname" text = "name" verticalalignment = "center" horizontalalignment = "center"> </textblock>
</GRID>
For the same effect, you can write the hidden mainpage file as follows:
// When the phoneapplicationpage is loaded, private void phoneapplicationpage_loaded (Object sender, routedeventargs e) {// textblock txtblk = new textblock (); txtblk. name = "txtname"; txtblk. TEXT = "name"; txtblk. verticalalignment = verticalignment. center; txtblk. horizontalalignment = horizontalalignment. center; contentpanel. children. add (txtblk );}
This event requires you to add the loaded event to the phoneapplicationpage class of The XAML file. In addition, we also find that we have added the textblock to the grid using the children attribute of the grid element named contentpanel, in fact, grid adds textblock to its children set;
Inheritance of attributes
Property inheritance is a feature of Silverlight. It can include some attributes in phoneappplicationpage (generally the forground attribute and font-related attributes, such as fontfamily, fronsize, fronstyle, frontweight, and fronstretch) inherit to mainpage. In the following example, we use phoneappplicationpage to set attributes.
Fontstyle = "italic"
We will see the following results:
We can see that all fonts in mainpage are changed to italics, And the textblock element displayed as "name" is changed:
<Textblock X: Name = "txtname" fontstyle = "normal" text = "name" verticalignment = "center" horizontalalignment = "center"> </textblock>
Then, the result is as follows:
So we can sum up the priority of the property: ① the highest priority set locally; ② the inherited property priority is centered; ③ the lowest by default
Attribute element syntax
Different writing methods of elements and their attributes. For example, the textblock element of "name" is displayed above. In XAML, there are several different writing formats:
Original method ①:
<Textblock X: Name = "txtname" fontstyle = "normal" text = "name" verticalignment = "center" horizontalalignment = "center"> </textblock>
Method 2:
<Textblock>
<Textblock. Name> txtname </textblock. Name>
<Textblock. Text> name </textblock. Text>
<Textblock. fontstyle> normal </textblock. fontstyle>
<Textblock. verticalignment> center </textblock. verticalignment>
<Textblock. horizontalalignment> center </textblock. horizontalalignment>
</Textblock>
The effects of the two writing methods are always the same. You can see that the writing method ② is more appealing, but this method is cumbersome. You can use the second method when appropriate. The name, text, fontstyle, verticalalignment, and horizontalalignmen are called attribute elements, which are represented by XML elements.. Net properties. Note that the attribute element tag cannot contain anything else;
Color and paint brush
We changed the element named txtname to the foreground attribute.
<Grid X: Name = "contentpanel" grid. Row = "1" margin = "12,0, 12,0">
<Textblock X: Name = "txtname" fontstyle = "normal" text = "name"
Verticalalignment = "center" horizontalalignment = "center"
Foreground = "# ff0000"
> </Textblock>
We can see the effect: here we use a red, green, and blue string to represent the color. Each color value ranges from 00 to FF and occupies a hexadecimal number of one byte, where 00 indicates full transparency, FF indicates that it is not transparent. For scrgb color space, the value is set between 0 and 1, which can be defined as follows:
<Textblock X: Name = "txtname" fontstyle = "normal" text = "name"
Verticalalignment = "center" horizontalalignment = "center"
Foreground = "SC #0.1, 0.2, 0.3"
> </Textblock>
Effect: For the difference between sRGB and scrgb, refer to the http://www.sudu.cn/info/index.php? OP = article & id = 275494
According to the attribute element syntax above, we can write as follows:
<Textblock>
<Textblock. Name> txtname </textblock. Name>
<Textblock. Text> name </textblock. Text>
<Textblock. fontstyle> normal </textblock. fontstyle>
<Textblock. verticalignment> center </textblock. verticalignment>
<Textblock. horizontalalignment> center </textblock. horizontalalignment>
<Textblock. Foreground>
<Solidcolorbrush>
<Color> SC #0.1, 0.2, 0.3 </color>
</Solidcolorbrush>
</Textblock. Foreground>
</Textblock>
In addition, the foreground attribute can be inherited through the visualization tree, but the background attribute cannot be inherited. The default value of the background attribute is null, And the null attribute is the same as the transparent attribute;
Source code