Property I change from the UI and then submit before popping a page gets changed to null when popped

I have a fairly complex model which I is bound to my UI but also is an object for my SQLite-Net table.

Here is the property I'm trying to change

[Table("MyObjectModel")]
public class MyObjectModel: INotifyPropertyChanged
{
    ...
    public string ObjectsType
    {
        get
         {
              return _objectsType;
         }

         set
         {
               _objectsType= value;
               OnPropertyChanged();
         }
      }
    ...
}

I change it by taking a copy of it using a shallow copy:

EditingItem = originalItem.NewCopy();

public MyObjectModel NewCopy()
{
  return (MyObjectModel)this.MemberwiseClone();
}

I then change the ObjectsType property as I want to, let's say to "STRINGVALUE".

The submit button then saves it to my repository and pops the page, passing the item back as a parameter:

        private async Task SubmitObjectEdit()
        {
              _objectItemRepository.UpdateObjectItem(EditingItem);
              await CoreMethods.PopPageModel(data: EditingItem, modal: true);
        }

On the popped to screen, I immediately put this passed object into a previous objects place in the observable collection. ObjectsType can be anything, let's say "PREVIOUSVALUE". It correctly gets set to "STRINGVALUE". However before execution is complete, the ObjectsType setter is called a final time and set to NULL. I check the call stack and see that this is still happening from the popped pages SubmitObjectEdit() method.

Why does this happen? It seems to be a binding issue or some type of problem with FreshMvvm's PopePageModel. But I'm not explicitly setting the item to null in code, so what gives?

Things I've tried:

Changing to oneway binding in both directions (does not work)
Setting the object property directly instead of copying the passed object into the observable collection (works, but I have to do this with many fields and it's very messy).

Answers

  • HarshitaHarshita INMember ✭✭✭

    on the setter property always check the null value
    set
    {
    if(value!=null)
    {
    _objectsType= value;
    OnPropertyChanged();
    }
    }

    and EditingItem = originalItem.NewCopy();
    you create EditingItem as a new object?

  • CharlieFinlayson.3926CharlieFinlayson.3926 USMember ✭✭
    edited January 9

    Hi @Harshita. This is the workaround I have employed. I just thought that there must be some bug with binding that causes it to be set to null like this. I'll probably accept it as the answer unless anyone else has any input on it. What if I actually want to set the item to null though?

    And yes, I create a new copy with NewCopy. I wanted to be sure I wasn't referencing the object from the previous page in the observable collection, but I wasn't sure how the references would work for this. It also allows me to cancel edits made to an object without having to undo them all manually.

Sign In or Register to comment.