【问题标题】:Xamarin.Forms ScrollView.ScrollToAsync inside DataTemplateDataTemplate 内的 Xamarin.Forms ScrollView.ScrollToAsync
【发布时间】:2020-06-05 02:57:40
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: xamarin.forms xamarin.ios


    【解决方案1】:

    您可以尝试创建ScrollViewbindableProperty 并将滚动值绑定到此属性,在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); }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多