I have been learning windows Metro development for a while recently. When I read more things, I feel a little trivial. So I decided to sort out the learning content for each period of time, by the way, you can also exercise your expression and generalization skills. This is a process of sustained and progressive progress.
First, let's list what we learned today:
Dynamic definition of style Background (tangled issues have plagued some time. It turns out to be the case ...)
XML file operations
Datetimeformatter time format
1. Dynamic definition of style Background
I believe many people have defined styles in page. Resources or app. Resources, which are easy to define and highly reusable. However, the background needs to be dynamically defined, for example, in the following scenarios: You can freely DIY page styles, such as the background, font size, and font color.
Style tbnewstyle = new windows. UI. XAML. style ();
Tbnewstyle. setters. Add (New setter (textblock. fontsizeproperty, "25 "));
Tbnewstyle. setters. Add (New setter (textblock. foregroundproperty, new solidcolorbrush (colors. Yellow )));
Tbnewstyle. targettype = typeof (textblock );
This. tbtext. Style = tbnewstyle;
The writing method is very simple. Today I have encountered a tangle of problems: tbnewstyle. setters. add (New setter (fontsizeproperty, "25") I used this method to dynamically modify the style. Later, I added textblock to another user's notebook. fontsizeproperty, because it is unique to the control attributes, it must be indicated which controls can take effect. Here, we also remind you that you must pay attention to it next time ~
Resources should be used to define resource styles in the project as much as possible. The code is highly reusable and easy to maintain.
2. xml file operations
The learning in this section is mainly an example of the parameter msdn.
(1) how to read XML files in a project
Storagefolder = await windows. ApplicationModel. Package. Current. installedlocation. getfolderasync (folder );
Storagefile = await storagefolder. getfileasync (File );
Xmlloadsettings loadsettings = new xmlloadsettings ();
Loadsettings. prohibitdtd = false;
Loadsettings. resolveexternals = false;
Xml‑t document = xmldocument. loadfromfileasync (storagefile, loadsettings );
String xmlstr = Document. getxml ();
(2) append a value to a node in XML
VaR Doc = new windows. Data. xml. Dom. xmldocument ();
VaR CDATA = Doc. createcdatasection (RSS );
VaR element = Doc. getelementsbytagname ("content"). Item (0 );
Element. appendchild (CDATA );
(3) modify the value of a node
The following example shows that the value of sell10day on the subnode in the Product node set is greater than the value of hot attribute in the instore node.
VaR Doc = new windows. Data. xml. Dom. xmldocument ();
VaR XPath = "/products/product [sell10day> instore]/@ Hot ";
VaR hotattributes = Doc. selectnodes (XPath );
For (uint Index = 0; index
{
Hotattributes. Item (INDEX). nodevalue = "1 ";
}
(4) Create and save an XML file
VaR Doc = new windows. Data. xml. Dom. xmldocument ();
Doc. loadxml (xmlstr );
Var file = await windows. Storage. applicationdata. Current. localfolder. createfileasync ("hotprodcuts. xml ");
Await Doc. savetofileasync (File );
There are many attributes in the xmldocument class. The usage is similar to that in. NET Framework. I will list them one by one later, such as appending a node or appending a property.
3. datetimeformatter time format
Win8 provides a wide range of time formatting methods, mainly using datetimeformatter for conversion.
Datetimeformatter formater = new datetimeformatter ("latest date") for example: longdate, interval time, longtime
Formater. Format (datetime );
Combination of various days, months, and years
Formater = new datetimeformatter ("day month ")
Format the display format of day, month, and year respectively
Formater = new datetimeformatter (
Yearformat. Full,
Monthformat. abbreviated,
Dayformat. Default,
Dayofweekformat. abbreviated)
For more formatting types, refer to the msdn example, which contains detailed instructions.
The above is most of the content I have learned today. There may be some problems in many details. I have not tried many attributes and methods for operations on the main XML file, in the future, I will study this part in depth, summarize it into every use case, and continue to work hard ~~