最近一段時間一直在學習Windows Metro的開發,看的東西多了就會覺得有些瑣碎,所以決定還是要將每段時間的學習內容梳理一下,順便也鍛煉一下自己的表達方式和概括能力。這是一個持續漸近的過程哦,一定要持之以恆下去。
先列一下今天學習的內容:
Style后台動態定義(糾結的問題困擾了些時間,原來它是那么回事。。。)
XML文件的操作
DateTimeFormatter時間格式化
1、 Style 后台動態定義
相信很多人都在Page.Resources或App.Resources中定義過Style,定義的方式簡便而重用性高。但有時也需后台動態定義,例如以下場景:用戶可自由DIY頁面樣式,如背景、字體大小、字體顏色等等。
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;
寫法很簡單,今天我遇到糾結的問題就是:tbNewStyle.Setters.Add(new Setter(FontSizeProperty,"25"))我用的這樣的寫法,怎么也動態修改不了樣式,后來也是在別人的提點下,才加進TextBlock.FontSizeProperty,因為對於對於控件的屬性都是獨有的,必須標明是哪類控件的屬性方能生效,在這兒也提醒各位一下哈,下次寫的時候一定要注意~
項目中還是應該盡量使用Resources定義資源樣式,代碼的復用性高,易於維護。
2、 XML文件的操作
對於本部分的學習主要還是參數MSDN的示例。
(1) 讀取項目中XML文件的方法
StorageFolder storageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(folder);
StorageFile storageFile = await storageFolder.GetFileAsync(file);
XmlLoadSettings loadSettings = new XmlLoadSettings();
loadSettings.ProhibitDtd = false;
loadSettings.ResolveExternals = false;
XmlDocumt document=XmlDocument.LoadFromFileAsync(storageFile, loadSettings);
String xmlStr=document.GetXml();
(2) 向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)修改某結點的值
以下示例是修改Product結點集中子結點Sell10day的值大於InStore的結點中hot屬性的值
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.Length; index++)
{
hotAttributes.Item(index).NodeValue = "1";
}
(4)創建XML文件並保存
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);
XmlDocument類中還有很多屬性,用法跟.net FrameWork中類似,后續我會一一列出來,如追加一個結點、追加屬性等。
3、 DateTimeFormatter 時間格式化
Win8中提供了非常豐富的時間格式化,種類繁多,主要用DateTimeFormatter進行轉換
DateTimeFormatter formater= new DateTimeFormatter("shortdate")如:longdate,shorttime,longtime
formater.Format(dateTime);
各種日月年的組合
formater =new DateTimeFormatter("day month")
分別格式化日月年的顯示格式
formater =new DateTimeFormatter(
YearFormat.Full,
MonthFormat.Abbreviated,
DayFormat.Default,
DayOfWeekFormat.Abbreviated)
更多格式化類型可以參考MSDN示例,里面有非常詳細的寫法
以上就是我今天所學習的大部分內容,可能很多細節的地方還有問題,主要XML文件的操作很多屬性和方法也沒有具體試用過,后面我會對該部分進行深入學習,盡量概括到每種使用場景,繼續努力~~