Windows 8 學習筆記(一)

來源:互聯網
上載者:User

最近一段時間一直在學習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檔案的操作很多屬性和方法也沒有具體試用過,後面我會對該部分進行深入學習,盡量概括到每種使用情境,繼續努力~~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.