我正在开发一个Xamarin Forms移动应用程序,它有一个包含SearchBar,ListView和Map控件的页面.列表视图包含一个地址列表,这些地址在地图上反映为引脚.
当用户在SearchBar中键入时,ListView会自动更新(通过ViewModel绑定).过滤列表数据源的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
}
Run Code Online (Sandbox Code Playgroud)
我希望更新Map,因为ListView被过滤,但不是在每个按键上,因为这可能每秒发生多次.基本上,我想在更新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]
Run Code Online (Sandbox Code Playgroud)
可以使用可移植类库中提供的Stopwatch类来完成此操作,但我怀疑使用Tasks可以实现更清晰/更好的方法.
任何人都可以建议一个"更聪明" PCL兼容的方式来做到这一点?
c# mvvm task-parallel-library portable-class-library xamarin.forms