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

Source: Internet
Author: User

Using local database and isolatestorage in Windows Phone-In mvvm mode (-), we have completed three things, including:

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.

 

 

Next we will continue to add the IM function .......

4. Add the sendmessage Function

We have designed messageview. XAML and bound msgviewmodel above. Next we will add several attributes in messageviewmodel to correspond to the input box and ListBox message list in view respectively. The attributes bound to the message input box are as follows,

   1: private string sendmessage;

   2: public string SendMessage

   3: {

   4:     get { return sendmessage; }

   5:     set { 

   6:         if(value!=null)

   7:         {

   8:             sendmessage=value;

   9:             RaisePropertyChanged("SendMessage");                

  10:         }

  11:     }

  12: }

 

Raisepropertychanged ("sendmessage"); is used to notify that the binding value has changed and is encapsulated by mvvmlight. It is basically consistent with the policypropertychanged implemented by userinfotable class.

   1: #region NotifyPropertyChanged

   2:  

   3: public event PropertyChangedEventHandler PropertyChanged;

   4: private void NotifyPropertyChanged(string propertyname)

   5: {

   6:     if(PropertyChanged!=null)

   7:     {

   8:         PropertyChanged(this,new PropertyChangedEventArgs(propertyname));

   9:     

  10:     }

  11:  

  12: }

  13:  #endregion

 

As for the source bound to ListBox, we need to use observablecollection <userinfotable>. Here, we do not use list <userinfotable> or ienumerable <userinfotable>, it is because when we bind ListBox, if itemsource is changed or re-instantiated, list or ienumerable can refresh ListBox, but if only one item in ListBox is changed, at this time, the list cannot meet the requirements. Some people say that usertable does not implement inotifypropertychanged or inotifypropertychanging. When the usertable Of the item is changed, ListBox can be refreshed. Well, this is correct, however, when list/ienumerable is used to add or delete values, The ListBox will not be updated.

At this time, we need to use the observablecollection. Of course, the item must also implement inotifypropertychanged and Other interfaces. See the following code:

   1: private ObservableCollection<UserInfoTable> userTable=new ObservableCollection<UserInfoTable>();

   2:  

   3: public ObservableCollection<UserInfoTable> UserTable

   4: {

   5:     get{return this.userTable; }

   6:     set {

   7:         if (value != null)

   8:         {

   9:             userTable = value;

  10:             RaisePropertyChanged("UserTable");

  11:         }

  12:     }

  13: }

Another note is that usertable in raisepropertychanged ("usertable") should not be written as usertable or as a field. Otherwise, debugging may fail.

Now we can add command for our send message button. Create in messageviewmodel

   1: publicICommandSendMessageCommand{get;privateset;}

Note: In WPF, mvvm MODE Command is a great achievement. Of course, the icommand must reference the using system. Windows. Input namespace.


 

Then, register the execute event of sendmessagecommand in the messageviewmodel constructor. First, reference the namespace

Using galasoft. mvvmlight. Command; we need to use the relaycommand and Lamda expressions (of course, you can also use function calls ). The Code is as follows:

   1:  

   2:             SendMessageCommand = new RelayCommand(            

   3:                 ()=>{

   4:                     UserInfoTable userinfo = new UserInfoTable();

   5:                     userinfo.UserId = 1;

   6:                     userinfo.UserNameAndTime = "Haisa";

   7:                     userinfo.UserMessage = sendmessage;

   8:                     userTable.Add(userinfo);

   9:                     haisaDataContext.HaisaTable.InsertOnSubmit(userinfo);

  10:                     haisaDataContext.SubmitChanges();

  11:                 }       

  12:             );

Now we have defined the command in viewmodel, and then use blend to bind the send message button and sendmessagecommand. For example, drag invokecommandaction in behaviours to the send message button, set the properties of invokecommandaction in the Properties window on the right:


 

 

For example, we only need to set eventname to click for this program.

OK. Now we have set the RESPONSE event of the sendmessage button. Let's look at the execute event of sendmessagecommand. We instantiate

Userinfotable class, and then make userinfo. usermessage = sendmesable, and then add it to observablecollection. In this case, you can update the sent messages in ListBox. We also want to store the sent messages as historical records, that is to say, each time you open a program, you can view the history of the content, then you need to use the local database. In the constructor, instantiate haisadatacontext = new haisadatacontext (haisadatacontext. dbconnectionstring). Then, in sendmessagecommand, you can use LINQ for database insert:

   1: haisaDataContext.HaisaTable.InsertOnSubmit(userinfo);

   2:  haisaDataContext.SubmitChanges();

Note that you must use the submitchanges function to write data to the database. Of course, this is just a simple simulation of sending message. In a short time, I will use socket to actually send message to the server. Pai_^

We also want to view the historical records of sent messages. In this case, we add a click event to applicationbariconbutton and add the code for navigation to messagehistoryview to the event. applicationbariconbutton seems unable to bind command, we directly Add the following event in behindcode:

   1: this.NavigationService.Navigate(new Uri("/View/MessageView.xaml", UriKind.RelativeOrAbsolute));

5. added the page function of messagehistoryview.

Messagehistoryview adds only one ListBox and one applicationbariconbutton that returns sendmessageview. The method for binding ListBox in messagehistoryview is similar to that in messageview, except that the Command Execution Code is changed to read the local database SQL ce.

As follows:

   1: ShowMessageHistoryCommand=new RelayCommand(

   2:         ()=>{

   3:              

   4:             IEnumerable<UserInfoTable> users=from p in haisaDataContext.HaisaTable select p;              

   5:             userTable = new ObservableCollection<UserInfoTable>(users);

   6:         

   7:         }

   8:         );

Because a viewmodel class is instantiated when each view is bound to a viewmodel, binding multiple views to the same viewmodel does not affect each other, because it is actually bound to multiple viewmodel instances.

At this time, the basic functions have been implemented, but is the user experience really good? We know that Windows Phone has a tombstone mechanism. If some processing is not performed on the tombstone mechanism, the user experience is rather bad. The next article will explain it.

6. Improve user experience

Coming soon .......

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.