【问题标题】:Xamarin Forms CollectionView ScrollTo Not working on LoadXamarin Forms CollectionView ScrollTo 无法加载
【发布时间】:2020-08-05 18:59:59
【问题描述】:

我有使用 CollectionView 的 XAML 表单。我将视图绑定到日期对象的集合。我在代码后面设置了 SelectedItem,它按预期工作。然后我尝试在后面的代码中使用 ScrollTo,但是一旦加载页面,它总是显示集合中的第一项。页面加载后,我连接了一个图像以触发 ScrollTo 事件,它按预期工作。下面是我的代码。提前感谢您的帮助!

'''
        for (int i = 0; i < 31 ; i++)
        {
            ScheduleDate date = new ScheduleDate();
            date._Date = dt.AddDays(i);
            colDates.Add(date);
        }

        cvDate.ItemsSource = colDates;
        cvDate.SelectedItem = colDates[10];
        cvDate.ScrollTo(cvDate.SelectedItem, null, ScrollToPosition.Start, true)
'''

XAML 表单

'''
            <CollectionView x:Name="cvDate" Grid.Row="0" Grid.Column="1" 
                SelectionMode="Single" >
                <CollectionView.ItemsLayout>
                    <LinearItemsLayout Orientation="Horizontal"  />
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding Description}"
                                            FontAttributes="Bold"
                                            FontSize="Large"
                                            HorizontalOptions="Center"
                                            VerticalOptions="Center"
                                            />
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
'''

编辑:

我连接了 OnCollectionViewScrolled 事件,当从 OnAppearing() 执行以下代码行时

cvDate.ScrollTo(cvDate.SelectedItem, null, ScrollToPosition.Start, false);

OnCollectionViewScrolled 未被调用。但是,当我从按钮单击事件中执行同一行代码时,OnCollectionViewScrolled 正在被触发。

【问题讨论】:

标签: xaml xamarin.forms


【解决方案1】:

我不确定您的 Description_Date 是什么。我做一个简单的例子供大家参考。

  1. 在 OnAppearing 覆盖方法中触发 ScrollTo:

    protected override void OnAppearing()
     {
        cvDate.ScrollTo(cvDate.SelectedItem, null, ScrollToPosition.Start, false);
     }
    
  2. 使用按钮点击方法触发 ScrollTo:

    private void Button_Clicked(object sender, EventArgs e)
     {
         cvDate.ScrollTo(cvDate.SelectedItem, null, ScrollToPosition.Start, false);
     }
    

整个代码:

public partial class Page4 : ContentPage
{
    public ObservableCollection<ScheduleDate> colDates { get; set; }
    public Page4()
    {
        InitializeComponent();

        colDates = new ObservableCollection<ScheduleDate>();
        for (int i = 0; i < 31; i++)
        {
            ScheduleDate date = new ScheduleDate();
            date.Description = i.ToString();
            colDates.Add(date);
        }

        cvDate.ItemsSource = colDates;
        cvDate.SelectedItem = colDates[10];
      

    }
    protected override void OnAppearing()
    {
       cvDate.ScrollTo(cvDate.SelectedItem, null, ScrollToPosition.Start, false);

    }
    private void Button_Clicked(object sender, EventArgs e)
    {
        //cvDate.ScrollTo(cvDate.SelectedItem, null, ScrollToPosition.Start, false);
    }
}
public class ScheduleDate
{
    public string Description { get; set; }
}

截图:

更新:

 <ContentPage.Content>

    <StackLayout>
        <CollectionView
        x:Name="cvDate"
        ItemsSource="{Binding colDates}"
        SelectionMode="Single">
            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Horizontal" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Label
                    FontAttributes="Bold"
                    FontSize="Large"
                    HorizontalOptions="Center"
                    Text="{Binding Description}"
                    VerticalOptions="Center" />
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
        <Button Clicked="Button_Clicked"></Button>
    </StackLayout>

</ContentPage.Content>

【讨论】:

  • 感谢您的回复,我已经完全复制了您的代码并且遇到了同样的问题。我可以看看你的 XAML 吗?
  • 我已经上传了我的 xaml。请检查一下。如果您对此仍有疑问,我会上传我的项目。
  • 我使用了您的确切代码,但仍然无法正常工作。话虽如此,我相信我明白了原因,我正在使用 MasterDetailPage 设置。当我将您的代码放入普通内容页面时,它会按预期工作。在这一点上,我猜这是一个错误。我将您的答案标记为正确。如果您有任何建议,我将不胜感激。谢谢大家的帮助!
  • 我在 shell 应用程序中遇到了同样的问题 - scrollto 不会抛出错误,但似乎什么也没做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 2019-11-08
  • 2020-02-16
  • 1970-01-01
相关资源
最近更新 更多