New Fashion Windows 8 Development (4): Save/read local application settings

Source: Internet
Author: User

Old week's blog column: http://blog.csdn.net/tcjiaan

For more information, see the original author and source.

 

 

Start Vs and open "Object Browser". Let's look for something.

Find the windows. Storage namespace in "Object Browser" and pay attention to the following classes.

Here, we should pay attention to applicationdata and check its name. You can also guess what line it is. Obviously, it must be related to read/write application settings.

Well, I guess it is correct. applicationdata has a static property -- current, which does not need to be explained. It refers to the application we are currently running. Therefore, we know that this attribute is used to obtain the applicationdata of the current application.

Okay, the first problem is solved. Now let's look at the second question: how to save the application settings? How to read? In fact, this is a problem. Once you know how to save it, you will naturally know how to read it.

Let's take a look at the localsettings attribute. It returns an applicationdatacontainer class instance, which is a container and can include N containers in the local settings of an application, however, each application also has a container. Therefore, if we only access simple settings and do not need to create a container, we can use the default setting container.

The applicationdatacontainer class has a values attribute. It returns an ipropertyset interface, which can be found through the "Object Browser". It is actually a dictionary (key-value). Therefore, "Object Browser" is very useful. It can solve many problems for us.

 

I believe that if you have a solid programming foundation, you don't have to read it down. Now, you know how to read and write application settings.

What are application settings used? Well, this is not hard to understand. For example, our application will provide a "Settings" page for users to set according to their habits, such as which skin the user chooses, or set the data on a page. The default value is 15 or 30.

The setting information is relatively simple data, so we can save it to the local settings.

 

In addition, we mentioned that the setting information has a container. You can think that the setting information group is the same as setting a folder in the storage area.

It can also be associated with a Registry. In Windows, the Registry is actually a huge dictionary. It has a key, and each key corresponds to a value. But we know that the Registry is grouped, just like the folders and files saved in our hard disk.

For example, the Registry root can be divided into HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, and so on.

 

Next, let's do an exercise together, so we don't just stay in theory. Let's do it first.

1. Start Vs and create an item (39 words are omitted here ).

2. Open mainpage. XAML and refer to the following XAML code:

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. columndefinitions> <columndefinition width = "2 *"/> <columndefinition width = "*"/> </grid. columndefinitions> <textblock grid. column = "0" margin = "20" textwrapping = "Wrap"> <span fontsize = "36" foreground = "pink" fontweight = "black"> example </span> <linebreak/> <run fontsize = "24"> select an item from the list on the right, then the application is suspended or exited, and then the application is run. </Run> <linebreak/> <run fontsize = "24"> after the selection is made, the index of the selected item is saved to the settings. When the page is initialized, read the index of the selected list item from the settings. </Run> </textblock> <ListBox X: Name = "mylistbox" fontsize = "28" selectionchanged = "onselectedchanged" grid. column = "1"> <listboxitem> Project 1 </listboxitem> <listboxitem> Project 2 </listboxitem> <listboxitem> Project 3 </listboxitem> <listboxitem> Project 4 </ listboxitem> <listboxitem> Project 5 </listboxitem> </ListBox> </GRID>

I believe it is not hard to understand. The focus is ListBox. We can set several items and bind their selectionchanged events.

3. Right-click the selectionchanged event and select "locate to event handler" from the pop-up menu. At this time, the event is transferred to the background code file.

4. I prefer to use C #. Therefore, the following code is C.

        private void onSelectedChanged(object sender, SelectionChangedEventArgs e)        {            ListBox lb = sender as ListBox;            if (lb != null)            {                if (lb.SelectedIndex > -1)                {                    ApplicationDataContainer myContainer = ApplicationData.Current.LocalSettings;                    myContainer.Values["index"] = lb.SelectedIndex;                }            }        }

The code is not complex. The focus is to see how to save the settings. Here, we save the index of the selected item.

5. When the application navigate to this page, we will read the saved index from the settings and restore the selected index of ListBox.

        protected override void OnNavigatedTo(NavigationEventArgs e)        {            ApplicationDataContainer container = ApplicationData.Current.LocalSettings;            if (container.Values.ContainsKey("index"))            {                this.MyListBox.SelectedIndex = (int)container.Values["index"];            }            else            {                this.MyListBox.SelectedIndex = 0;            }        }

Before reading the settings, you must use the containskey method to determine whether the key to be searched exists. Only when the key exists can the value be read.

6. Now let's run it.

Run. In the list on the right, select any item. At this time, the setting information is saved.

Return to vs, and in the "debugging position" in the toolbar, "suspend and end the program ".

 

Wait until the application ends and run again. At this time, you will see that the index selected by ListBox is the index value we saved just now.

In this way, the Read and Write settings are achieved.

 

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.