How to Use Crosslight to Properly Save a SelectedItem in a DataListViewModelBase?

6 replies. Last post: September 23, 2015 8:30 AM by Nicholas Lie
Tags :
  • (None)
  • New Discussion
  • New Question
  • New Product Feedback
Jimmy TungolMember

Hi,

How do we properly use DataListViewModelBase.SaveDataAsync() method from a modal context? We need to update and save changes to the SelectedItem of a PARENT DataListViewModel, after saving data from a context view (e.g. List A > List B > Editor A > Modal List A > Modal Editor A). When a change has been made to an Item in Modal Editor A, we need to manipulate the SelectedItem on List A and should be saved after the changes on Modal Editor A has been executed (e.g. Modal Editor A.ExecuteSave() followed by List A.SelectedIndex.ExecuteSave()).

We currently implemented this behavior using an event aggregator, which sends a message to List A viewmodel to start manipulating the SelectedItem data and call SaveDataAsync(). See code snippet below, this code doesn't work if the SelectedItem being manipulated is a newly added item. Doesn't work in the sense that the changes are not being saved to the database even though the item clearly changed while it's in the device. Any attempt to save changes to the new item fails; however, once you restart (close > open) the app... any changes being made will be saved to the database.

        private async void ExecuteSave(object parameter)
        {
            try
            {
                // Get the collected date and current date/time.
                var p_CollectedDate = this.SelectedItem.CollectedDate;
                var p_CurrentDate = DateTime.Now;

                if (p_CollectedDate == null)
                {
                    // Update the collected date if it's null or empty.
                    this.SelectedItem.CollectedDate = p_CurrentDate.Date;
                    this.SelectedItem.TimeIn = p_CurrentDate;
                }

                // Always notify the date/time of change.
                this.SelectedItem.ModifiedOn = DateTime.UtcNow;

                // Commit changes to entity.
                this.SelectedItem.EndEdit();

                // Update the value to the database.
                this.Repository.Update(this.SelectedItem);

                // Raise an item changed event.
                this.OnDataChanged(this.SelectedItem);

                // Save the changes to the database.
                await this.SaveDataAsync();

                // Raise an item changed event.
                this.OnDataChanged(this.SelectedItem);
            }
            catch (Exception p_Exception)
            {
                Debug.WriteLine(p_Exception.Message);
            }
        }

 

All times are GMT -5. The time now is 1:23 PM.
Previous Next