Xamarin.Forms ScrollView.ScrollToAsync内部数据模板

Xamarin.Forms ScrollView.ScrollToAsync内部数据模板,xamarin.forms,xamarin.ios,Xamarin.forms,Xamarin.ios,我在ListView的DataTemplate中有ScrollView。我需要水平自动滚动每个项目的scrollview(到由ListItemView绑定到的ViewModel内的值确定的不同点) 据我所知,没有办法绑定滚动位置。如何在DataTemplate内的ScrollView上调用ScrollView.ScrollToAsync方法 谢谢 您可以尝试创建ScrollView的bindableProperty并将滚动值绑定到此属性,在propertyChanged事件中调用ScrollTo

我在ListView的DataTemplate中有ScrollView。我需要水平自动滚动每个项目的scrollview(到由ListItemView绑定到的ViewModel内的值确定的不同点)

据我所知,没有办法绑定滚动位置。如何在DataTemplate内的ScrollView上调用ScrollView.ScrollToAsync方法


谢谢

您可以尝试创建
ScrollView
bindableProperty
并将滚动值绑定到此属性,在
propertyChanged
事件中调用
ScrollToAsync
方法:

public class CustomScrollView : ScrollView
{
    public static readonly BindableProperty offSetProperty = BindableProperty.Create(
        propertyName: nameof(offSet),
        returnType: typeof(int),
        declaringType: typeof(CustomScrollView),
        defaultValue: 0,
        defaultBindingMode: BindingMode.TwoWay,
        propertyChanged: ScrollOffsetChanged
    );

    static void ScrollOffsetChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var view = (CustomScrollView)bindable;//here you should check if the bindable is CustomScrollView, I don't see your xaml
        view.offSet = (int)newValue;
        view.ScrollToAsync(view.offSet, 0, true);
    }

    public int offSet
    {
        get { return (int)GetValue(offSetProperty); }
        set { SetValue(offSetProperty, value); }
    }
}