Windows Phone (12) Try to customize the style

Source: Internet
Author: User

Styles are often used in BS development. In WP, the system also provides a solution, that is, to share the resources of the Set styles. The first is to share the resource location, it is in the app class. We have already introduced how to set public attributes to store temporary data. For details, refer to three data sharing methods for Windows Phone (8 ), the shared style is also implemented in the app class, and the system is in the app. the resources set is provided in the XAML file:

<! -- Application resources -->
<Application. Resources>

</Application. Resources>

We only need to add our custom style to the above label. The object for this resource has a class derived from frameworkelement. The list of such Derived classes is as follows:

 

Microsoft. Internal. Rule. Controls. visualtreegraft
System. Windows. Controls. Border
System. Windows. Controls. contentpresenter
System. Windows. Controls. Control
System. Windows. Controls. drawingsurface
System. Windows. Controls. Image
System. Windows. Controls. itemspresenter
System. Windows. Controls. mediaelement
System. Windows. Controls. multiscaleimage
System. Windows. Controls. Panel
System. Windows. Controls. primitives. Popup
System. Windows. Controls. richtextblock
System. Windows. Controls. richtextblockoverflow
System. Windows. Controls. textblock
System. Windows. Controls. viewbox
System. Windows. Controls. webbrowser
System. Windows. Documents. glyphs
System. Windows. shapes. Shape

 

This shared resource can be used for all classes derived from the above or above classes. This refers to a custom style. Next, follow the practices in the previous article to set a foreground color for the textblock In the content area, so in app. the custom XAML style can be as follows:

<! -- Application resources --> <application. resources> <lineargradientbrush X: key = "lgbrush"> <gradientstop offset = "0" color = "aliceblue"> </gradientstop> <gradientstop offset = "1" color = "burlywood"> </gradientstop> </lineargradientbrush> </application. resources>

X: The key feature is a key name that uniquely identifies a resource and must be unique in shared resources. It is cumbersome to use custom styles, do not advocate:

<Textblock X: Name = "tbcontent" text = "display style" horizontalalignment = "center" verticalignment = "center"> <textblock. foreground> <staticresource resourcekey = "lgbrush"> </staticresource> </textblock. foreground> </textblock>

The common writing is as follows:

<Textblock X: Name = "tbcontent" text = "display style" horizontalalignment = "center" verticalignment = "center" foreground = "{staticresource lgbrush}"> </textblock>

It can be seen that there is a braces, which are the XAML Markup extensions. quotation marks cannot be used in the XAML Markup extensions. For example, the lgbrush here cannot use quotation marks; the above two methods achieve the same effect:

In addition, we can see that the mainpage class has defined foreground in the XAML file, but we still see the custom color in tbcontent, this indicates that the style setting priority is higher than the inherited style priority. The above two methods are used for style in the XAML file. We can also hide the file (. CS), but it must be after the constructor is complete. For example, we can access the newly defined style as follows:

Using system; using system. collections. generic; using system. LINQ; using system. net; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. shapes; using Microsoft. phone. controls; namespace using style {public partial class mainpage: phoneapplicationpage {// constructor public mainpage () {initializecomponent (); lineargradientbrush lgbrush = (lineargradientbrush) This. resources ["lgbrush"]; textblock TB = new textblock (); TB. name = "tbname"; TB. verticalalignment = verticalignment. center; TB. horizontalalignment = horizontalalignment. center; TB. TEXT = "Hide code instantiation"; TB. foreground = lgbrush; this. contentpanel. children. add (TB );}}}

If you want to use this style, just like the code above instantiate the style and set the foreground color of textblock to lgbrush, another way is to change X: Key in the Custom style to X: name: You can set the foreground color in the hidden file as follows: (there is a question: According to the document, I cannot obtain the set color)

TB. Foreground = lgbrush;

You do not need to instantiate the custom color. Note that if you use the X: Name resource, the name must be unique in the XAML file;

The preceding example shows how to customize a foreground style. The following describes how to define a style set for a specific element.

<phone:PhoneApplicationPage><phone:PhoneApplicationPage.Resources>        <Style x:Key="tbStyle" TargetType="TextBlock">         <Setter Property="HorizontalAlignment" Value="Center"></Setter>        </Style>    </phone:PhoneApplicationPage.Resources></phone:PhoneApplicationPage>

In the above example code, X: Key indicates the key name, which is used when the style is used. targettype indicates the object element used for this style, in the STYLE tag, the setter tag is used to set the element attributes that apply this style;

Instance code:

<phone:PhoneApplicationPage>    <phone:PhoneApplicationPage.Resources>        <Style x:Key="tbStyle" TargetType="TextBlock">            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="Foreground">                <Setter.Value>                    <LinearGradientBrush>                        <GradientStop Offset="0.2" Color="Brown"></GradientStop>                        <GradientStop Offset="0.7" Color="DarkBlue"></GradientStop>                    </LinearGradientBrush>                </Setter.Value>            </Setter>        </Style>    </phone:PhoneApplicationPage.Resources></phone:PhoneApplicationPage>

Use the XAML tag extension in the textblock element to get the style:

<Textblock X: Name = "tbcontent" text = "display style" horizontalalignment = "center" verticalignment = "center" style = "{staticresource tbstyle}"/>

The result is as follows:

 

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.