MAUI 更新 CollectionView 事件倒计时

发布于 2025-01-11 22:52:57 字数 1410 浏览 0 评论 0原文

您好,我正在尝试进行活动倒计时,但遇到了一些问题。

该函数被调用:

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

isCounting 在 OnAppearing() 中设置为 true

然后:

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 只是一个 Timespan

前端的绑定中途有效 - name 属性看起来很好,但倒计时字符串永远不会更新。如果我将 itemsource 设置为 null,然后将其更改回我的 observablecollection 源,它会更新..但随后我的内存不断增加,并且似乎不是执行此操作的正确方法。

我的调试几乎显示了我想在前端看到的信息。 我在这里做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-01-18 22:52:57

在第一条评论的帮助下,我能够通过在我的模型上添加带有 PropertyChanged 的​​设置器来解决这个问题。

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

以及以下事件:

public event PropertyChangedEventHandler PropertyChanged;

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

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));
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文