Use local database and isolatestorage in Windows Phone-In mvvm mode (3)

Source: Internet
Author: User

In the previous two articles, we used local database and isolatestorage in Windows Phone-In mvvm mode (-) and Windows Phone-In mvvm mode (2) completed the first few steps of the project for sending messages like Im:

1. Create a Windows Phone project for mvvmlight. 2. Create an SQL ce database in the model. 3. Create the messageview. XAML interface and bind the messageviewmodel. 4. Add the sendmessage Function 5. added the page function of messagehistoryview.

 

In fact, in addition to adding a socket to send a message (I will re-write an article to explain the socket), our task has basically been completed. In the mobile Internet era, user experience is king. We will discuss the tombstone from this point:

6. Improve user experience

First, let's look at the Windows Phone program lifecycle:

When the WP7 program is executed, press the START key to switch to the backend and enter the dormant status. It can be understood as memory usage, but it is not running in the background. In this case, the program can be quickly switched back. At this time, we do not need to perform any operations. If WP7 has insufficient memory (mango seems to allow five programs to be In the dormant state), it will release the memory occupied by the background program, that is, it enters the tombstoned status. If the information on the interface is not saved when you enter the tombstone, the information on the interface will be lost when you press the back key to return the information from the tombstone mechanism. At this time, the user will be confused: Does WP7 not support multi-task processing? I just want to pause the program. Why is the information lost?

What we expect is:

 

 

 

But the actual operation is true: (when the tombstone operation is not added)

 

 

 

If you want to make the program return from the tombstone status consistent with that when entering the tombstone status, that is, the user cannot detect whether or not he has exited the Program, providing such a seamless user experience, we need to add some operations to save the current status during development. It is officially recommended that both page entry and exit occur.Onnavigatedto andOnnavigatedfromStore and restore page data in the event. We also recommend this, but now we want to use the page Load And Unload events. When the tombstone status occurs, the load and unload events also occur, this also provides other ways to operate the tombstone. Therefore, we only need to create two new commands to bind the load and unload events. Add two eventtocommands on phoneapplicationpage, set eventname to loaded, and bind the command to the two commands created in messageviewmodel:

   1: public ICommand PageLoadCommand{get;private set;}

   2: public ICommand PageUnloadCommand{get;private set;}

 

 

Next we will save the status of sendmessage view when unload page is used: Here we only need to save the status in the current ListBox and send message input boxes.

See the following code:

   1: PageUnloadCommand = new RelayCommand(

   2:     () =>

   3:     {

   4:         IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings;

   5:         if (ISSetting.Contains("SendMessage"))

   6:         {

   7:             ISSetting["SendMessage"] = SendMessage;

   8:         }

   9:         else

  10:         {

  11:             ISSetting.Add("SendMessage", SendMessage);

  12:         }

  13:  

  14:  

  15:         if (ISSetting.Contains("MessageList"))

  16:         {

  17:             ISSetting["MessageList"] = UserTable;

  18:         }

  19:         else

  20:         {

  21:             ISSetting.Add("MessageList", UserTable);

  22:         }

  23:         

  24:     }

  25:     );

In this way, saving and reading key values is relatively cumbersome, and the codes in the corresponding function ERA can be abstracted to be concise, Which is skipped here.

When loading page, read the previously saved status:

   1: PageLoadCommand = new RelayCommand(

   2:     () =>

   3:     {

   4:         IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings;

   5:         if (ISSetting.Contains("SendMessage"))

   6:         {

   7:             SendMessage = (string)ISSetting["SendMessage"];

   8:         }

   9:         if (ISSetting.Contains("MessageList"))

  10:         {

  11:             ObservableCollection<UserInfoTable> users = (ObservableCollection<UserInfoTable>)ISSetting["MessageList"];

  12:             UserTable = users;

  13:         }        

  14:  

  15:     }

  16:     );

 

At this time, the tombstone mechanism of our page navigation has been added, and the functions expected by the above users have been completed. When debug wants to test the tombstone, right-click the project and choose Properties From the shortcut menu to go to the project properties page.

Tombstone upon deactivation while debugging can be tested on the simulator.

 

Click here to download the Code:

Postscript:

1. In mvvm mode, you can also use functions in APP. XAML. CS.

Private void application_deactivated (Object sender, deactivatedeventargs E)

The viewmodel is saved in, and the workload will be very large at this time. This method is not recommended;

2. the difference between Windows Phone and Android is that when you press the Home Key to return to the home page and click the back key, the program you just executed will be returned. When you click the Home Key to return to the home page, if you click the program icon, Android reads the cache from the memory like the back key and returns to the place where the program was executed, while Windows Phone restarts a new program instance ...... At this time, users who are used to other systems will be relatively confused. This is convenient when you want to execute multiple program instances at the same time. However, if you want to click the icon and only allow one instance at the same time, we need to add more operations by our developers. In addition to the tombstone mentioned above, we also need to save the URI or other flag of the currently opened page, save it to isolatestorage when the tombstone or unexpectedly exits, and then in

Private void application_launching (Object sender, launchingeventargs E)

Function to determine whether to restore the tombstone status or re-open a new app instance. This issue will be resolved in detail in subsequent blog posts. Pai_^

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