New Fashion Windows 8 Development (3): Application Status

Source: Internet
Author: User

Old week's blog column:

For more information, see the original author and source.


As you may remember, we have discussed application status in WP development. Similarly, in Win8 development, we can also use this item as appropriate. In the previous section, we learned from the application cycle that when our "Panel" application is not running on the foreground, it will be suspended.

The pending behavior is similar to that in WP. In WP, the current status of the application does not mean that the application will be lost every time it is placed in the background, the system maintains the status of five applications for us. Once the number of applications stacked in the background exceeds five, I'm sorry. Well, we should consider saving the application status as appropriate.

This is also the case in Win8. However, it is not as strict as WP Control, unless you manually remove the application process or exit the application.


Note: The application state we mentioned here is only valid at runtime. The data is not saved to the hard disk or storage device, but only in the memory, the role is to facilitate data transmission.

In the past winform development or WPF development, we will consider using a static class or declaring some static fields in a class to save global data, that is, adding the static keyword during the declaration.

In addition to this method, we may wish to use this class in Win8 development today.

Start Vs and open "Object Browser". We can find the coreapplication class, which is located in the windows. ApplicationModel. Core namespace. See the graph.


I marked it on the graph. Is there a Blue Circle? There is a static property properties, which is actually a dictionary structure. Well, we should understand the dictionary structure, that is, one key and one value.

Now, we can use it to store some runtime information. You can regard it as a session in ASP. NET.

First, let's take a simple example of how to play the game. The gameplay is very simple. If you have one player, you can do it yourself.

After running the example, enter two values on the first page, and save the two values to the coreapplication. properties dictionary;

The values are displayed on the second and third pages respectively.

To view multiple views on the same page, I put a frame on the home page and use this frame to display other pages. The homepage's XAML is as follows.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. columndefinitions> <columndefinition width = "Auto"/> <columndefinition/> </grid. columndefinitions> <ListBox grid. column = "0" verticalalignment = "stretch" width = "200" fontsize = "28" selectionchanged = "listbox_selectionchanged" selectionmode = "single"> <listboxitem> page 1 </listboxitem> <listboxitem> page 2 </listboxitem> <listboxitem> page 3 </listboxitem> </ListBox> <frame X: name = "myframe" grid. column = "1"/> </GRID>

The C # code in the background is to navigate Based on the page selected in ListBox.

Private void listbox_selectionchanged (Object sender, selectionchangedeventargs e) {listboxitem item = E. addeditems [0] As listboxitem; If (item! = NULL) {string STR = item. content as string; Switch (STR) {Case "Page 1": myframe. navigate (typeof (page1); break; Case "Page 2": myframe. navigate (typeof (page2); break; Case "Page 3": myframe. navigate (typeof (page3); break; default: Break ;}}}

Next, let's start with the first page, which is used to input data and save the status.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <textblock grid. row = "0" margin = "25" text = "first page" style = "{staticresource headertextstyle}"/> <stackpanel margin = "15" grid. row = "1"> <textblock text = "enter two status values on this page. The first status is obtained in the second State on the page. The second status value is obtained in the third State on the page. "Style =" {staticresource groupheadertextstyle} "/> <textblock text =" Enter the first State value: "margin =, "Style =" {staticresource bodytextstyle} "/> <textbox name =" txt1 "margin =", "/> <textblock text =" enter the second status value: "margin =", "style =" {staticresource bodytextstyle} "/> <textbox name =" txt2 "margin =, 220 "/> <button margin =", 20, "width =" "content =" Save status "Click =" onsave "/> </stackpanel> </GRID>


We want to process the Click Event of the Save button.

        private void onSave(object sender, RoutedEventArgs e)        {            if (string.IsNullOrWhiteSpace(txt1.Text)                || string.IsNullOrWhiteSpace(txt2.Text))            {                return;            }            Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] = txt1.Text;            Windows.ApplicationModel.Core.CoreApplication.Properties["value2"] = txt2.Text;        }

The next two pages are almost the same. They are read from the saved status of the first page.

Page 2-XAML

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <textblock grid. row = "0" margin = "25" text = "second page" style = "{staticresource headertextstyle}"/> <textblock name = "TB" fontsize = "32" margin = "20, 20, 0, 0 "grid. row = "1"/> </GRID>

Page 2-C #

        protected override void OnNavigatedTo(NavigationEventArgs e)        {            if (Windows.ApplicationModel.Core.CoreApplication.Properties.ContainsKey("value1"))            {                this.tb.Text = Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] as string;            }        }


Page 3-XAML

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <textblock grid. row = "0" margin = "25" text = "Third page" style = "{staticresource headertextstyle}"/> <textblock name = "TB" grid. row = "1" margin = "20, 20, 0, 0" fontsize = "32"/> </GRID>

Page 3-C #

        protected override void OnNavigatedTo(NavigationEventArgs e)        {            if (Windows.ApplicationModel.Core.CoreApplication.Properties.ContainsKey("value2"))            {                this.tb.Text = Windows.ApplicationModel.Core.CoreApplication.Properties["value2"] as string;            }        }

Run it. First, select page 1 in the list on the left of the home page and enter two data items. Remember to click the Save button.

Go to page 2 and page 3 to see if the result is displayed?


Is this simple? Of course, during the test, we also found that when the program is suspended and then returned, the input content in the text box is gone, but the content saved to coreapplication. properties can still be read.

Of course, if you exit the application, the State that is stored naturally will also be lost. Therefore, if you keep the data in the text box after the program is suspended, you should know how to do it.

If you want to read the configuration information after the program exits, you need to save the data to the hard disk.


Next we will talk about another thing related to the running status-page parameter transfer.

When you see this, you will surely remember to pass Parameters on the WP page. At that time, we also talked about URI ing. Do you remember? Forget the relationship, because we do not need URI ing today.

In Win8 "Panel" application development, the page parameter delivery parameter is implemented by calling the navigate method of the frame class. It has two overloading methods, one of which can be used to pass parameters, that is

public bool Navigate(System.Type sourcePageType, object parameter)

We can see that this parameter is of the object type, which makes it very difficult to develop. We can transmit strings, numbers, and so on. We can also define a class to transfer more data, you can also directly transfer a class instance.


Let's get started with an exercise.

1. Start cute vs. A Windows Store application in the new project is the Win8 application.

2. After creating a project, you will see that the app. XAML. CS file is opened by default.

3. Add a class as you like. You can simply add a public attribute.

    public class Product    {        public string ProductName { get; set; }        public string ProductID { get; set; }    }

4. Create a new page named pageget. XAML. You just need to remember it.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel margin = "30"> <textblock text = "parameters passed through the page: "Style =" {staticresource headertextstyle} "/> <stackpanel orientation =" horizontal "margin =", "> <textblock text =" product No: "fontsize =" 28 "/> <textblock name =" tbproid "fontsize =" 28 "/> </stackpanel> <stackpanel orientation =" horizontal "margin =" 5, 13, 0, 0 "> <textblock text =" Product Name: "fontsize =" 28 "/> <textblock name =" tbproname "fontsize =" 28 "/> </stackpanel> </GRID>

Right-click the XAML document and select "view code" from the menu ".

Protected override void onnavigatedto (navigationeventargs e) {// get the Parameter Product = E. parameter as product; If (product! = NULL) {This. tbproid. Text = product. productid; this. tbproname. Text = product. productname ;}}

5. Go back and open mainpage. XAML. Let's layout it.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel margin = "30"> <textblock text = "Enter the product ID: "Style =" {staticresource subheadertextstyle} "/> <textbox name =" txtid "margin =", "/> <textblock margin =, 0, 0 "text =" Enter the product name: "style =" {staticresource subheadertextstyle} "/> <textbox name =" txtname "margin =, 5, 0 "/> <button content =" jump "padding =" 35, 10, 35, 10 "margin =" 15, 20, "click =" onnav "/> </stackpanel> </GRID>

Click an event to process the button.

Private void onnav (Object sender, routedeventargs e) {// retrieve the root frame of the current window if (window. Current. Content is frame & window. Current. content! = NULL) {frame myframe = Window. current. content as frame; product PRD = new product () {productid = txtid. text, productname = txtname. text}; // navigate to the target page and pass the myframe parameter. navigate (typeof (pageget), PRD );}}

Now let's run it.




Related Article

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: 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.