在最近的幾周裏,咱們結合老師在課堂上所講的知識,以及課下在經過視頻學習的內容,經過Win8.1 App、WP8.1 App以及WPF的三種形式,聯繫了C#的一些具體的編程方法。下面咱們就來回顧一下在編程過程當中所使用到的一些方法:node
(一)、頁面之間的跳轉:編程
對於這一部分,在編寫Windows App時和編寫WPF是所使用的方法是不一樣的,下面就分別來介紹一下:函數
1.Windows App:學習
Frame.Navigate(typeof(DetailPage), file1); //跳轉至名爲DetailPage的頁面 //並將當前頁面的file1做爲參數傳到DetailPage中。
2.WPF:this
NavigationService.GetNavigationService(this).Navigate(new Uri("DetailPage.xaml", UriKind.Relative)); //一樣是跳轉到名爲DetailPage的界面 //與Windows App中不一樣的是,它不支持向下一頁傳遞參數。
因爲WPF中不支持向下一頁傳遞參數,那麼我所想到的解決方法是在建立一個環境變量,在每次頁面跳轉時經過對環境變量賦值,來起到傳遞參數的做用。spa
(二)、數據的存儲與調用:code
一樣的,因爲Windows App與WPF編程之間存在不一樣,這裏我也使用到了不一樣的方法。視頻
1.Windows App:xml
首先是在Windows App中,我選擇的方法是對於每個要存儲的項目建立一個相應的file,並以項目的名稱來對file進行命名,file的內部存儲項目的具體信息。程序實現方法以下:blog
1 StorageFolder storage = await ApplicationData.Current.LocalFolder.CreateFolderAsync("MemoryList", CreationCollisionOption.OpenIfExists); 2 XmlDocument _doc = new XmlDocument(); 3 XmlElement _item = _doc.CreateElement("Element"); 4 _item.SetAttribute("Date", DateBox.Date.Year + "." + DateBox.Date.Month + "." + DateBox.Date.Day); 5 _item.SetAttribute("First", FirstBox.Text); 6 _item.SetAttribute("Second", SecondBox.Text); 7 _item.SetAttribute("Third", ThirdBox.Text); 8 _doc.AppendChild(_item); 9 10 StorageFile file = await storage.CreateFileAsync(DateBox.Date.Year + "." + DateBox.Date.Month + "." + DateBox.Date.Day + ".xml", CreationCollisionOption.ReplaceExisting); 11 await _doc.SaveToFileAsync(file);
在這裏,首先是建立了一個用來儲存每一個項目的file的folder,而後對file內部的各個element進行了賦值,最後將file命名並保存。這樣一來在個人Listview中藥展現我已有的項目時就只要遍歷個人folder中的全部文件,並將它們的文件名顯示出來便可。實現方法以下:
1 StorageFolder storage = await ApplicationData.Current.LocalFolder.CreateFolderAsync("MemoryList", CreationCollisionOption.OpenIfExists); 2 OutputBox.Items.Clear(); 3 var files = await storage.GetFilesAsync(); 4 { 5 foreach (StorageFile file in files) 6 { 7 Grid a = new Grid(); 8 ColumnDefinition col = new ColumnDefinition(); 9 GridLength gl = new GridLength(200); 10 col.Width = gl; 11 a.ColumnDefinitions.Add(col); 12 ColumnDefinition col2 = new ColumnDefinition(); 13 GridLength gl2 = new GridLength(200); 14 col2.Width = gl; 15 a.ColumnDefinitions.Add(col2); 16 TextBlock txbx = new TextBlock(); 17 txbx.Text = file.DisplayName; 18 Grid.SetColumn(txbx, 0); 19 HyperlinkButton btn = new HyperlinkButton(); 20 btn.Width = 100; 21 btn.Content = "Detail"; 22 btn.Name = file.DisplayName; 23 btn.Click += (s, ea) => 24 { 25 Frame.Navigate(typeof(DetailPage), file); 26 }; 27 Grid.SetColumn(btn, 1); 28 29 a.Children.Add(txbx); 30 a.Children.Add(btn); 31 32 OutputBox.Items.Add(a); 33 } 34 }
最終所達到的效果大概是這樣:
圖中紅色方框內即是遍歷了folder中的各個file後並將file名稱顯示出來的結果。
2.WPF:
在WPF中因爲不能使用Windows.Storage這一方法,因此我不得不改變策略,選擇了經過建立一個Xml文件,並將每一個項目的信息做爲文件中根節點下的子節點來儲存。這樣的方法實現過程以下:
1 String title = DateBox.SelectedDate.Value.ToString().Split(' ')[0]; 2 xmlDoc = new XmlDocument(); 3 xmlDoc.Load("Events.xml"); 4 XmlNode root = xmlDoc.SelectSingleNode("Events"); 5 XmlNodeList nodeList = xmlDoc.SelectSingleNode("Events").ChildNodes; 6 foreach (XmlNode xn in nodeList) 7 { 8 XmlElement xe = (XmlElement)xn; 9 if (xe.GetAttribute("Date") == title ) 10 root.RemoveChild(xe); 11 } 12 XmlElement _item = xmlDoc.CreateElement("Event"); 13 _item.SetAttribute("Date", DateBox.SelectedDate.Value.ToString().Split(' ')[0]); 14 _item.SetAttribute("First", FirstBox.Text); 15 _item.SetAttribute("Second", SecondBox.Text); 16 _item.SetAttribute("Third", ThirdBox.Text); 17 root.AppendChild(_item); 18 xmlDoc.Save("Events.xml");
因爲在Xml文件中存儲不像存儲file時,系統會自動對文件名相同的文件進行覆蓋,這裏爲了不出現多個同名項目,我在存儲前先判斷力是否與當前項目同名的子節點,若是有,則先刪除原有項目在存 儲新項目。
在顯示時的方法與Windows App中所用到的大體相同,也是遍歷全部項目,不一樣的是這裏是遍歷了根節點下的全部子節點,並將子節點輸出出來,固然使用的函數也稍有區別:
1 OutputBox.Items.Clear(); 2 xmlDoc = new XmlDocument(); 3 xmlDoc.Load("Events.xml"); 4 XmlNodeList nodeList = xmlDoc.SelectSingleNode("Events").ChildNodes; 5 foreach (XmlNode xn in nodeList) 6 { 7 XmlElement xe = (XmlElement)xn; 8 9 Grid evet = new Grid(); 10 ColumnDefinition col = new ColumnDefinition(); 11 GridLength gl = new GridLength(220); 12 col.Width = gl; 13 evet.ColumnDefinitions.Add(col); 14 ColumnDefinition col2 = new ColumnDefinition(); 15 GridLength gl2 = new GridLength(220); 16 col2.Width = gl; 17 evet.ColumnDefinitions.Add(col2); 18 TextBlock txbx = new TextBlock(); 19 txbx.FontSize = 25; 20 txbx.Text = xe.GetAttribute("Date"); 21 Grid.SetColumn(txbx, 0); 22 Button btn = new Button(); 23 btn.Width = 150; 24 btn.FontSize = 15; 25 btn.Content = "See Details"; 26 btn.Click += (s, ea) => 27 { 28 App.elementDetail = xe; 29 NavigationService.GetNavigationService(this).Navigate(new Uri("DetailPage.xaml", UriKind.Relative)); 30 }; 31 Grid.SetColumn(btn, 1); 32 evet.Children.Add(txbx); 33 evet.Children.Add(btn); 34 OutputBox.Items.Add(evet); 35 }
上述就是我在最近幾周的實驗中所學習到的一些知識,在從此的學習生活中我將繼續學習新的方法,挑戰更有難度的程序,並繼續分享本身在其中的心得體會。