Liam's C # Learning Journey (ix): Experimental Learning

Source: Internet
Author: User

In the last few weeks, we have contacted some of the specific programming methods of C # through the Win8.1 app, the WP8.1 app, and three forms of WPF, in combination with what our teachers have learned in class and what we learn through video in class. Let's review some of the methods used in the programming process:

(i), the jump between pages:

For this part, when writing Windows apps and writing WPF, the methods used are different, as described below:

1.Windows App:

Frame.navigate (typeof(detailpage), file1);    // jump to a page                                               named Detailpage // and file1 the current page as a parameter. the Detailpage. 

2.WPF:

Navigationservice.getnavigationservice (this). Navigate (new Uri ("detailpage.xaml", urikind.relative));  // also jump to the interface                                                            named Detailpage                             // Unlike in Windows apps, it does not support passing parameters to the next page.   

Because the next-page pass parameter is not supported in WPF, the solution I've thought of is to create an environment variable that will pass the parameters when each page jumps by assigning a value to the environment variable.

(b), the storage and invocation of data:

Again, because of the difference between the Windows app and WPF programming, I've used a different approach here.

1.Windows App:

First of all in the Windows app, I chose to create a corresponding file for each item to be stored, and name the file as the project, and file the details of the internal storage project. Program implementation methods are as follows:

1Storagefolder storage =awaitApplicationData.Current.LocalFolder.CreateFolderAsync ("memorylist", creationcollisionoption.openifexists);2XmlDocument _doc =NewXmlDocument ();3XmlElement _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 TenStorageFile file =awaitStorage. Createfileasync (DateBox.Date.Year +"."+ DateBox.Date.Month +"."+ DateBox.Date.Day +". XML", creationcollisionoption.replaceexisting); One await_doc. Savetofileasync (file);

Here, the first is to create a folder to store the file for each project, then assign the individual element inside the file, and finally name and save the file. This way I can just walk through all the files in my folder and display their filenames when I show my existing projects in my ListView. The implementation method is as follows:

1Storagefolder storage =awaitApplicationData.Current.LocalFolder.CreateFolderAsync ("memorylist", creationcollisionoption.openifexists);2 OutputBox.Items.Clear ();3             varFiles =awaitstorage. Getfilesasync ();4             {5                 foreach(StorageFile fileinchfiles)6                 {7Grid A =NewGrid ();8ColumnDefinition col =Newcolumndefinition ();9GridLength gl =NewGridLength ( $);TenCol. Width =GL; One A.columndefinitions.add (col); AColumnDefinition col2 =Newcolumndefinition (); -GridLength GL2 =NewGridLength ( $); -Col2. Width =GL; the A.columndefinitions.add (col2); -TextBlock txbx =NewTextBlock (); -Txbx. Text =file. DisplayName; -Grid.setcolumn (TXBX,0); +Hyperlinkbutton BTN =NewHyperlinkbutton (); -Btn. Width = -; +Btn. Content ="Detail"; ABtn. Name =file. DisplayName; atBtn. Click + = (s, ea) = = -                     { -Frame.navigate (typeof(detailpage), file); -                     }; -Grid.setcolumn (BTN,1); -  in A.children.add (txbx); - A.children.add (BTN); to  + OutputBox.Items.Add (a); -                 } the}

The result is probably this:

The red box in the figure is the result of traversing each file in the folder and displaying the file name.

2.WPF:

Because of the inability to use the Windows.storage method in WPF, I had to change the policy by creating an XML file and storing the information for each project as a child node under the root node of the file. The implementation of this method is as follows:

1String title = DateBox.SelectedDate.Value.ToString (). Split (' ')[0];2xmldoc =NewXmlDocument ();3Xmldoc.load ("Events.xml");4XmlNode root = Xmldoc.selectsinglenode ("Events");5XmlNodeList nodeList = Xmldoc.selectsinglenode ("Events"). ChildNodes;6             foreach(XmlNode xninchnodeList)7             {8XmlElement XE =(XmlElement) xn;9                 if(XE. GetAttribute ("Date") ==title)Ten Root. RemoveChild (XE); One             } AXmlElement _item = xmldoc.createelement ("Event"); -_item. SetAttribute ("Date", DateBox.SelectedDate.Value.ToString (). Split (' ')[0]); -_item. SetAttribute (" First", firstbox.text); the_item. SetAttribute ("Second", secondbox.text); -_item. SetAttribute ("Third", thirdbox.text); - Root. AppendChild (_item); -Xmldoc.save ("Events.xml");

Since storing files in an XML file is not like storing a file, the system will automatically overwrite it, in order to avoid multiple items with the same name, if I have a child node with the same name as the current project before storing it, then delete the original item before storing the new item.

The method used in the display is roughly the same as in Windows app, and it is traversing all the items, except that it iterates through all the child nodes under the root node and outputs the child nodes, and of course uses a slightly different function:

1 OutputBox.Items.Clear ();2xmldoc =NewXmlDocument ();3Xmldoc.load ("Events.xml");4XmlNodeList nodeList = Xmldoc.selectsinglenode ("Events"). ChildNodes;5             foreach(XmlNode xninchnodeList)6             {7XmlElement XE =(XmlElement) xn;8 9Grid Evet =NewGrid ();TenColumnDefinition col =Newcolumndefinition (); OneGridLength gl =NewGridLength ( -); ACol. Width =GL; - Evet. Columndefinitions.add (col); -ColumnDefinition col2 =Newcolumndefinition (); theGridLength GL2 =NewGridLength ( -); -Col2. Width =GL; - Evet. Columndefinitions.add (col2); -TextBlock txbx =NewTextBlock (); +Txbx. FontSize = -; -Txbx. Text = Xe. GetAttribute ("Date"); +Grid.setcolumn (TXBX,0); AButton BTN =NewButton (); atBtn. Width = Max; -Btn. FontSize = the; -Btn. Content ="See Details"; -Btn. Click + = (s, ea) = = -                 { -App.elementdetail =XE; inNavigationservice.getnavigationservice ( This). Navigate (NewUri ("Detailpage.xaml", urikind.relative)); -                 }; toGrid.setcolumn (BTN,1); + Evet. Children.add (TXBX); - Evet. Children.add (BTN); the OutputBox.Items.Add (Evet); *}

These are some of the things I've learned in my experiments in recent weeks, and I'll continue to learn new ways to challenge more difficult programs and continue to share my experience in the future.

Liam's C # Learning Journey (ix): Experimental Learning

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.