【发布时间】:2018-11-22 23:57:24
【问题描述】:
在 Xamarin Forms 3.3 上,我无法在 iOS 上将 ListView 滚动到顶部。
我试试:Scrolling to start of Xamarin Forms ListView with header 但不能将滚动 ListView 设置为顶部。 这是我的代码:
在控制 iOS:
public class ExtListViewRenderer : ListViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e) => iOS.Control.Logger.InvokeAction(() =>
{
base.OnElementChanged(e);
if (!(e.NewElement is ExtListView extListView) || Control == null)
return;
extListView.EventScrollToTop += OnScrollToTop;
});
public void OnScrollToTop(object sender, EventArgs eventArgs)
{
Control.ScrollRectToVisible(new CoreGraphics.CGRect(0, 0, 1, 1), true);
}
}
课堂控制:
public class ExtListView : ListView
{
public event EventHandler EventScrollToTop;
/// <summary>
/// Scroll LisView To Top
/// </summary>
/// <param name="animate"></param>
public void ScrollToTop(bool animate = true)
{
//bool animate is not used at this stage, it's always animated.
EventScrollToTop?.Invoke(this, EventArgs.Empty);
}
}
在视图 .xaml:
<control:ExtListView.Behaviors>
<common:MethodToDelegateBehavior Delegate="{Binding ScrollToTop}"
DelegateType="{x:Type propertyViewModels:PropertyPageViewModel+ScrollToTopDelegate}"
MethodName="ScrollToTop" />
</control:ExtListView.Behaviors>
在页面:
public delegate void ScrollToTopDelegate(bool animate);
private ScrollToTopDelegate _scrollToTop;
public ScrollToTopDelegate ScrollToTop
{
get => _scrollToTop;
set => this.RaiseAndSetIfChanged(ref _scrollToTop, value);
}
方法使用时:
ScrollToTop?.Invoke(true);
- 我在 Android 上使用它可以工作,但 iOS 不工作。
【问题讨论】:
-
我在没有使用 ScrollToTopDelegate 的情况下测试了您的代码,它运行良好。当我调用
ScrollToTop函数时,listView 滚动到顶部。您能否提供更多信息以重现您的问题?
标签: listview xamarin xamarin.forms xamarin.ios