简体   繁体   English

MAUI 更新 CollectionView 事件倒计时

[英]MAUI Updating CollectionView event countdown

Hello I am attempting to make an event countdown but running into some issues.您好,我正在尝试进行事件倒计时,但遇到了一些问题。

This function gets called:这个 function 被称为:

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() isCounting 在 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 Tspan 只是一个时间跨度

The binding on front end halfway works - the name property appears fine but the countdown string never updates.前端的绑定中途工作 - name 属性看起来很好,但倒计时字符串永远不会更新。 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.如果我将 itemsource 设置为 null 然后将其更改回我的 observablecollection 源,它会更新..但是我的 memory 不断增加,并且似乎不是执行此操作的正确方法。

My debug pretty much shows the information I want to see on the front end.我的调试几乎显示了我想在前端看到的信息。 What am I doing wrong here?我在这里做错了什么?

With the help of the first comment I was able to figure this out by adding a setter with PropertyChanged on my model.在第一条评论的帮助下,我能够通过在我的 model 上添加一个带有 PropertyChanged 的 setter 来解决这个问题。

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));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM