Mufacka
Mufacka

Reputation: 257

MAUI Updating CollectionView event countdown

Hello I am attempting to make an event countdown but running into some issues.

This function gets called:

private void RunEventCountdowns()
            {
                    Device.StartTimer(new TimeSpan(0, 0, 1), () =>
                    {
                        if (isCounting)
                        {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            OnPropertyChanged("propChanged");
                        });
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                    });
            }

isCounting is set true in OnAppearing()

Then:

public event PropertyChangedEventHandler propChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = propChanged;
            foreach (var evt in EventList)
            {
                tSpan = evt.Date - DateTime.Now;
                evt.Time = tSpan;
                System.Diagnostics.Debug.WriteLine(evt.Name + " is this far away: " + evt.Time.ToString());
            }
        }

Tspan is just a Timespan

The binding on front end halfway works - the name property appears fine but the countdown string never updates. If I set itemsource to null then change it back to my observablecollection source it updates..but then my memory constantly increases, and doesn't seem like the proper way to do this.

My debug pretty much shows the information I want to see on the front end. What am I doing wrong here?

Upvotes: 0

Views: 1014

Answers (1)

Mufacka
Mufacka

Reputation: 257

With the help of the first comment I was able to figure this out by adding a setter with PropertyChanged on my model.

public TimeSpan Time { get { return time; } set { time = value; OnPropertyChanged("Time"); } }

As well as the following event:

public event PropertyChangedEventHandler PropertyChanged;

  private void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Upvotes: 0

Related Questions