【发布时间】:2020-02-27 13:06:01
【问题描述】:
我有一个托管CollectionView 和Map 的页面。
有一个项目列表,CollectionView 的ItemsSource 设置为该列表并基于此列表绘制地图图钉,要求当用户滚动CollectionView 时,突出显示对面的地图图钉,当点击 pin 时,CollectionView 会滚动到该项目。
我使用ScrollTo 和Scrolled 事件。但问题是当ScrollTo 被调用时,Scrolled 事件也会被触发,这会导致延迟或一些意外行为。
我试图设置一个标志:
private int centerIndex = -1;
bool userScroll;
private void CollectionView_Scrolled(object sender, ItemsViewScrolledEventArgs args)
{
if (centerIndex != args.CenterItemIndex)
{
if (userScroll)
MessagingCenter.Send<object, int>(this, Keys.GoToLocation, args.CenterItemIndex);
userScroll = true;
centerIndex = args.CenterItemIndex;
}
}
private void ScrollToVehicle(object arg1, Item item)
{
if (collectionView.ItemsSource != null && collectionView.ItemsSource.Cast<Item>().Contains(item))
{
userScroll = false;
collectionView.ScrollTo(item, position: ScrollToPosition.Center, animate: false);
}
}
但问题是Scrolled 事件被多次调用(在if 语句内)
【问题讨论】:
-
我猜你想在滚动到特殊位置时突出显示,你可以使用
ScrollTo方法而不是Scrolled。 -
@WendyZang-MSFT 我需要同时使用两者。我不想在请求
ScrollTo时触发Scrolled事件
标签: xamarin.forms collectionview