触发动作以在X毫秒后开始 [英] Trigger an action to start after X milliseconds

查看:84
本文介绍了触发动作以在X毫秒后开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Xamarin Forms移动应用程序,该应用程序的页面包含SearchBar,ListView和Map控件.列表视图包含地址列表,这些地址反映为地图上的图钉.

I'm developing a Xamarin Forms mobile app, which has a page containing a SearchBar, a ListView, and Map control. The list view contains a list of addresses, which are reflected as pins on the map.

当用户在SearchBar中键入内容时,ListView会自动更新(通过ViewModel绑定).筛选列表的数据源的ViewModel方法看起来像这样...

As the user types in the SearchBar, the ListView is automatically updated (through ViewModel binding). The ViewModel method that filters the datasource for the list looks something like this...

void FilterList()
{
    listDataSource = new ObservableCollection<location>(
        locationData.Where(l => l.Address.Contains(searchBar.Text))
    );

    // ***** Now, update the map pins using the data in listDataSource
}

我想在过滤ListView时更新Map,但不要在每次按键时更新,因为每秒可能会发生多次.本质上,在地图更新之前,我希望每个FilterList事件都有一个滚动暂停".用伪代码...

I want to update the Map as the ListView is filtered, but not on EVERY keypress as that could happen multiple times a second. Essentially, I want a "rolling pause" in each FilterList event before the Map is updated. In pseudo-code...

    // ***** Now, update the map pins using the data in listDataSource
    if (a previously-requested map update is pending)
    {
        // Cancel the pending map update request
    }

    // Request a new map update in 1000ms using [listDataSource]

可以使用可移植类库中提供的 Stopwatch 类来执行此操作,但我怀疑可以使用 Tasks 来实现此目的的更干净/更好的方法.

It's possible to do this using the Stopwatch class, which is available in Portable Class Libraries, but I suspect there's a cleaner/better way to accomplish this using Tasks.

有人可以建议一种聪明的" 与PCL兼容的方法吗?

Can anyone suggest a "cleverer" PCL-compatible way to do this?

推荐答案

简单延迟的问题在于,当延迟到期时,您可以排队等待一堆事件.您可以添加逻辑以丢弃在延迟运行时引发的事件.如果要在更高级别上声明逻辑,则可以使用反应性扩展.

The problem with simple delays is that you can get a bunch of events queued up for when the delay expires. You can add logic to throw away the events that were raised while the delay was running. If you want to declare the logic at a higher level, you can use Reactive Extensions.

这篇关于触发动作以在X毫秒后开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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