0

I'm having an object in my viewmodel which consists of an Id and some other attributes. my equality check contains the check on Id, so I can identify items in lists when refreshing.

public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != GetType()) return false;
            return Equals(obj as Class);
        }
public bool Equals(Class other)
        {
            if (other == null)
            {
                return false;
            }

            return this.Id.Equals(other.Id);
        }

Now when I'm on a detailPage which is only having one item, I want to manually refresh that item from the database. When doing my refresh the Id obviously does not change, but the remaining attributes can. Now the problem I have is that the Propertychanged is not triggered when item is identical(based on id).

Class refreshedItem = await Task.Run(() => backendConnection.GetSchedule(scheduleId, DTOType.Maxi, BackEndSettings.Default.SystemOfMeasurement));
Item = refreshedItem;

If I set the Item to a dummy item in between it is triggered every time.

Class refreshedItem = await Task.Run(() => backendConnection.GetSchedule(scheduleId, DTOType.Maxi, BackEndSettings.Default.SystemOfMeasurement));
Item = new Class(Guid.Empty) { Title = "test" };
Item = refreshedItem;
Michael O
  • 67
  • 5
  • You can manually tell it that the property changed. Without seeing more code, I'm not sure exactly what you need to do, perhaps `OnPropertyChanged("Item");`. See [Xamarin Forms - force a control to refresh value from binding](https://stackoverflow.com/questions/42124944/xamarin-forms-force-a-control-to-refresh-value-from-binding) – ToolmakerSteve Apr 13 '22 at 16:16
  • @ToolmakerSteve Yes I know that I can manually trigger a refresh, but I need to understand why it is not automatically triggered because it thinks its the same item. – Michael O Apr 14 '22 at 07:09

1 Answers1

1

Looking at Github Fody Properties Overview, the (default) code that is injected in Item setter is equivalent to

if (value != item)
    OnPropertyChanged();

It only fires when not equal.

The documentation for Fody Properties says to use Attributes to control its behavior. For what you want, use DoNotCheckEqualityAttribute.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196