c# - X 毫秒后触发一个 Action 开始

标签 c# mvvm task-parallel-library portable-class-library xamarin.forms

我正在开发一个 Xamarin Forms 移动应用程序,它有一个包含 SearchBar、ListView 和 Map 控件的页面。 ListView 包含一个地址列表,这些地址在 map 上显示为图钉。

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

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 ,但不是在每次按键时更新 map ,因为这可能每秒发生多次。本质上,我希望在 map 更新之前在每个 FilterList 事件中有一个“滚动暂停”。在伪代码中...

    // ***** 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 有更清晰/更好的方法来完成此操作。

谁能建议一种“更聪明”的PCL 兼容方法来做到这一点?

最佳答案

你可以试试:

await Task.Delay(2000);

关于c# - X 毫秒后触发一个 Action 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25176528/

相关文章:

ios - 当我使用 MvxSimpleTableViewSource 时,xamarin ios Tableview 没有重新加载

c# - 分布式深度优先搜索

c# - 使用几个不同的参数减少非常长的构造函数重载的数量

c# - 使用锁的正确位置在哪里

c# - 将值传递给 IValueConverter

c# - WPF - UI 未从命令更新

.net - Dataflow LinkTo 参数discardMessages 发生了什么?

c# - ActionBlock 可以包含状态吗?

c# - 没有控制台窗口的 AppServiceBridge

c# - Windows Phone 数据绑定(bind)列表选择器到字符串列表