3

我有一个如下图所示的列表视图。如您所见,我有一列名为Time。我想知道如何自动更新时间。

例如航班代码 ABC123 的时间应该在 1 分钟后从 3 分钟变为 4 分钟。

航班列表视图

4

3 回答 3

1

大型智能视图模型、哑视图和任何模型,最好的 MVVM 方法?包含 WPF 应用程序的代码,其中 ViewModel(和绑定到它的视图)由计时器更新。它每 1 秒(1000 毫秒)出现一次

于 2013-04-20T16:45:22.060 回答
1

Edit 对每分钟都使绑定无效的想法不满意(这可能完全没有根据,并且每分钟都无效,我不知道),所以想出了一个直接设置内容的“更纯粹”的实现,保留原件以供参考:

您可以通过 来完成AttachedBehaviour,下面应该可以帮助您入门:

public static class TimeAgoBehaviour {

    static Lazy<DispatcherTimer> LazyTimer = new Lazy<DispatcherTimer>(() => {
        DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMinutes(1) };
        timer.Start();
        return timer;
    });
    static ConcurrentDictionary<int, EventHandler> Events = new ConcurrentDictionary<int, EventHandler>();

    public static DateTime GetTimeAgo(ContentControl obj) {
        return (DateTime)obj.GetValue(TimeAgoProperty);
    }

    public static void SetTimeAgo(ContentControl obj, DependencyProperty value) {
        obj.SetValue(TimeAgoProperty, value);
    }

    public static readonly DependencyProperty TimeAgoProperty = DependencyProperty.RegisterAttached("TimeAgo",
        typeof(DateTime), typeof(TimeAgoBehaviour),
        new UIPropertyMetadata(DateTime.UtcNow, OnTimeAgoChanged));

    private static void OnTimeAgoChanged(object sender, DependencyPropertyChangedEventArgs e) {
        var newDate = (DateTime)e.NewValue;

        EventHandler oldEvent;
        if (Events.TryRemove(sender.GetHashCode(), out oldEvent)) {
            LazyTimer.Value.Tick -= oldEvent;
        }

        if (DateTime.MinValue == newDate) {
            return;
        }

        var doUpdate = new EventHandler((s, args) => {
            ContentControl control = sender as ContentControl;
            control.Content = TimeAgoConverter.GetTimeAgo(newDate);
        });

        doUpdate(sender, new EventArgs());

        Events.TryAdd(sender.GetHashCode(), doUpdate);
        LazyTimer.Value.Tick += doUpdate;
    }
}

xml:

<Label local:TimeAgoBehaviour.TimeAgo="{Binding InitialisedDate}" />



以下是原回复:


您可以通过 来执行此操作AttachedBehaviour,它每分钟都会使绑定无效,导致重新评估它,下面的工作,作为您需要的起点扩展:

代码:

public static class PropertyToInvalidateBehaviour {

    static Lazy<DispatcherTimer> LazyTimer = new Lazy<DispatcherTimer>(() => {
        DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMinutes(1) };
        timer.Start();
        return timer;
    });
    static ConcurrentDictionary<int, EventHandler> Events = new ConcurrentDictionary<int, EventHandler>();

    public static DependencyProperty GetPropertyToInvalidate(DependencyObject obj) {
        return (DependencyProperty)obj.GetValue(PropertyToInvalidateProperty);
    }

    public static void SetPropertyToInvalidate(DependencyObject obj, DependencyProperty value) {
        obj.SetValue(PropertyToInvalidateProperty, value);
    }

    public static readonly DependencyProperty PropertyToInvalidateProperty = DependencyProperty.RegisterAttached("PropertyToInvalidate",
      typeof(DependencyProperty), typeof(PropertyToInvalidateBehaviour),
      new UIPropertyMetadata(null, OnPropertyToInvalidateChanged));

    private static void OnPropertyToInvalidateChanged(object sender, DependencyPropertyChangedEventArgs e) {
        var propertyToInvalidate = e.NewValue as DependencyProperty;

        EventHandler oldEvent;
        if (Events.TryRemove(e.Property.GetHashCode(), out oldEvent)) {
            LazyTimer.Value.Tick -= oldEvent;
        }

        if (null == propertyToInvalidate) {
            return;
        }

        var doUpdate = new EventHandler((s, args) => {
            BindingExpression binding = BindingOperations.GetBindingExpression(sender as DependencyObject, propertyToInvalidate);
            binding.UpdateTarget();
        });

        Events.TryAdd(e.Property.GetHashCode(), doUpdate);
        LazyTimer.Value.Tick += doUpdate;
    }
}

xml:

<Label Content="{Binding Path=InitialisedDate, Converter={StaticResource cnvTimeAgo}}" local:PropertyToInvalidateBehaviour.PropertyToInvalidate="Label.Content" />`
于 2013-04-20T17:11:11.977 回答
1

查看调度计时器并每分钟执行一次记录更新

这就是我会做的

于 2013-04-20T13:46:43.277 回答