绑定到依赖于时间的属性 [英] Binding to time-dependent properties

查看:20
本文介绍了绑定到依赖于时间的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我写了一个类似小部件的应用程序,它应该跟踪任务,每个任务都有一个指定为 DateTime 的截止日期,现在如果你想显示多少时间直到截止日期您可能想要绑定到虚拟"(*curses virtual 关键字*)属性,如下所示:

Some time ago i wrote a small widget-like application which was supposed to keep track of tasks, each task had a deadline specified as a DateTime, now if you want to display how much time is left until the deadline you might want to bind to a "virtual" (*curses the virtual keyword*) property like this:

public TimeSpan TimeLeft
{
    get { return Deadline - DateTime.Now; }
}

显然,从理论上讲,此属性在每个滴答声中都会更改,您希望时不时更新您的 UI(例如,通过定期为该属性抽取 PropertyChanged 事件).

Obviously in theory this property changes every tick and you want to update your UI every now and then (e.g. by periodically pumping out a PropertyChanged event for that property).

回到当我编写小部件时,我每分钟刷新整个任务列表,但这并不理想,因为如果用户与某些项目交互(例如,通过键入绑定到 Comments 的 TextBox-property) 将被严重中断并且对源的更新丢失.

Back when i wrote the widget i refreshed the whole task list every minute, but this is hardly ideal since if the user interacts with some item (e.g. by typing in a TextBox which binds to a Comments-property) that will be harshly interupted and updates to the source get lost.

如果您有这样的时间相关属性,那么更新 UI 的最佳方法是什么?

So what might be the best approach to updating the UI if you have time-dependent properties like this?

(顺便说一下,我不再使用那个应用程序了,只是觉得这是一个很有趣的问题)

推荐答案

我认为您在代码示例后的第一段中所说的内容是在 WPF 中实现此功能的唯一合理方法.设置一个只为 TimeLeft 属性调用 PropertyChanged 的计时器.间隔会根据您的情况而有所不同(如果您谈论的是每周任务列表,您可能只需要每 5 分钟左右更新一次.如果您要谈论接下来 30 分钟的任务列表,您可能需要每分钟或 30 秒更新一次.

I think what you said in your first paragraph after the code sample is the only reasonable way to make this work in WPF. Set up a timer that Just calls PropertyChanged for the TimeLeft property. The interval would vary based upon your scenario (if you're talking a weekly task list, you probably only need to update it ever 5 minutes or so. If you're talking a task list for the next 30 minutes, you may need to update it every minute or 30 seconds or something.

该方法将避免您提到的刷新选项问题,因为只有 TimeLeft 绑定会受到影响.如果你有数百万个这样的任务,我想性能损失会非常显着.但是,如果您只有几十个或其他东西,那么每 30 秒左右更新一次这些绑定将是一个非常微不足道的问题,对吗?

That method would avoid the problems you mentioned with the refresh option since only the TimeLeft bindings would be affected. If you had millions of these tasks, I guess the performance penalty would be pretty significant. But if you only had a few dozen or something, updating those bindings every 30 seconds or so would be be a pretty insignificant issue, right?

我能想到的所有可能性都使用定时器或动画.当您将任务添加到列表中时,动画会过于繁重".而在 Timer 场景中,上面的场景似乎是最干净、最简单、最实用的.可能只是归结为它是否适用于您的特定场景.

Every possibility that I can think of uses either Timers or Animations. The animations would be way too "heavy" as you add tasks to the list. And of the Timer scenarios, the one above seems to be the cleanest, simplest and most practical. Probably just comes down to whether it even works or not for your specific scenario.

这篇关于绑定到依赖于时间的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆