简体   繁体   English

WPF绑定定期更新

[英]Wpf binding update regularly

I've got a complex back-end process that runs for a long period of time. 我有一个运行很长时间的复杂后端流程。 All the vital stats are exposed as properties with INotifyPropertyChanged events raised when they're set. 设置这些属性时,所有重要统计信息都会作为属性公开,并带有INotifyPropertyChanged事件。

I wanted to add an "Elapsed time" which is proving far more challenging than anticipated. 我想添加一个“经过的时间”,事实证明这比预期的要困难得多。

I exposed a ProcessStartedAt DateTime and threw together a converter to subtract the bound time from DateTime.Now . 我公开了一个ProcessStartedAt DateTime,并把一个转换器放在一起,以便从DateTime.Now减去绑定时间。 This works perfectly (once). 这完美(一次)。 After that, since the property isn't changing, the binding is never updated and the converter isn't called again. 此后,由于属性未更改,因此绑定永远不会更新,并且不会再次调用转换器。

I don't want the back-end to have a timer dedicated to updating an "Elapsed time" property. 我不希望后端有专门用于更新“经过的时间”属性的计时器。 It violates the separation of concerns principle. 它违反了关注点分离原则。 I'd be happier with a timer in the UI but am unsure how to force a binding to refresh without updating the property value. 我会更喜欢UI中的计时器,但不确定如何在不更新属性值的情况下强制刷新绑定。

Even better, is there a way I can tell the binding to refresh at regular intervals? 更好的是,有什么方法可以告诉绑定定期刷新吗?

<TextBlock Text="{Binding Path=ProcessStartedAt,
                          Converter={StaticResource ElapsedTime}}"/>

I would throw the timer inside the ValueConverter class and raise an event to call the Convert method each time the interval is hit. 我会在ValueConverter类中放入计时器,并在每次命中间隔时引发一个事件以调用Convert方法。

This keeps that ValueConverter focused on its responsibility. 这使ValueConverter始终专注于其责任。

No need for converter, just notify delaying on the getter: 无需转换器,只需在getter上通知延迟:

public string RemainingTime
{
    get {
           Task.Delay(1000).
           ContinueWith(w => OnPropertyChanged(() => RemainingTime));
           return (CurrentServerTime - SelectedDeliveryTime).ToString("mm\\:ss");
        }
}

The binding will be recalled because of the OnPropertyChange 由于OnPropertyChange,将撤回该绑定

'I've got a complex back-end process that runs for a long period of time.' “我有一个复杂的后端流程,需要长时间运行。” and 'I don't want the back-end to have a timer dedicated to updating an "Elapsed time" property.' 和“我不希望后端有专门用于更新“经过时间”属性的计时器。” seems a little counterproductive. 似乎适得其反。

Trying to not update the view with your view-model seems to violate some MVC principles. 尝试不使用您的视图模型更新视图似乎违反了某些MVC原则。 Suppose you find a way to do so, the following engineer/developer might not. 假设您找到了一种方法,以下工程师/开发人员可能没有。

Maybe you should find a way to timely change your view-model class timely instead of trying to 'block' updates in a timely manner. 也许您应该找到一种及时更改视图模型类的方法,而不是试图及时“阻止”更新。 In that way you wouldn't have to concern about how things are updated. 这样,您就不必担心事物如何更新。 Instead, you would 'apply' changes to your view-model in a way that are easy to understand, change in the future and also be able to update technology when updates do framework are applied. 取而代之的是,您将以一种易于理解的方式将更改“应用”到您的视图模型 ,以便将来进行更改,并且还可以在应用更新框架时更新技术。

EDIT 编辑

I guess the only way is to set a DispatcherTimer, subscribe to it and call the OnPropertyChanged. 我猜唯一的方法是设置一个DispatcherTimer,订阅它并调用OnPropertyChanged。 Not related to your question, but you can also put a Delay to your Binding if needed. 与您的问题无关,但是如果需要,您还可以在“绑定”中添加“延迟”。

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

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