Xamarin.forms Xamarin CollectionView-以编程方式滚动
我有收藏视图。请参阅下面的代码Xamarin.forms Xamarin CollectionView-以编程方式滚动,xamarin.forms,Xamarin.forms,我有收藏视图。请参阅下面的代码 <CollectionView ItemsSource="{Binding photos}" HeightRequest="300" ItemSizingStrategy="MeasureAllItems" Margin="10,0,10,0"
<CollectionView ItemsSource="{Binding photos}" HeightRequest="300"
ItemSizingStrategy="MeasureAllItems" Margin="10,0,10,0"
x:Name="photosView"
ItemsUpdatingScrollMode="KeepScrollOffset">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="3" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="2">
<Frame BackgroundColor="Red" HeightRequest="79" WidthRequest="53" Padding="0" Margin="0"
IsClippedToBounds="True" HasShadow="False" CornerRadius="10">
<Image Aspect="AspectFill" Source="{Binding imageUrl}"/>
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
void OnScrollMore(System.Object sender, System.EventArgs e)
{
//photosView.SendScrolled(new ItemsViewScrolledEventArgs() { FirstVisibleItemIndex = 0 });
photosView.ScrollTo(photosView.Y + 10, position: ScrollToPosition.MakeVisible, animate: true);
}
但什么都没有发生。它不会滚动到下一行
有什么建议吗?您的ScrollTo方法不起作用的原因是PhotoView在您的PhotoView项目资源中找不到项目“PhotoView.Y+10”。您正在调用的方法正在尝试在ItemsSource中查找项。它不会像您尝试的那样滚动到y位置。当转到定义时,可以在方法的描述中看到它。它正在等待“对象项”
public void ScrollTo(object item, object group = null, ScrollToPosition position = ScrollToPosition.MakeVisible, bool animate = true);
如果您试图做的是滚动到collectionview的最后一个添加项,那么尝试这种工作方法并从那里构建它。在这里,每次按下按钮,都会添加一个项目(字符串)。此项设置为按钮单击处理程序末尾的ScrollTo对象
MainPage.xaml
<StackLayout Orientation="Vertical">
<CollectionView ItemsSource="{Binding photos}" HeightRequest="300"
ItemSizingStrategy="MeasureAllItems" Margin="10,0,10,0"
x:Name="photosView"
ItemsUpdatingScrollMode="KeepLastItemInView">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="3" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="2">
<Frame BackgroundColor="Red" HeightRequest="79" WidthRequest="53" Padding="0" Margin="0"
IsClippedToBounds="True" HasShadow="False" CornerRadius="10">
<Label Text="{Binding}" TextColor="White"
HorizontalOptions="Center" VerticalOptions="Center"
HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="scroll more" HorizontalOptions="Center" VerticalOptions="End" Clicked="OnScrollMore"/>
</StackLayout>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
ObservableCollection<string> ObservableItems { get; set; }
public MainPage()
{
InitializeComponent();
ObservableItems = new ObservableCollection<string>(new List<string>() { "een", "twee", "drie" });
photosView.ItemsSource = ObservableItems;
}
void OnScrollMore(System.Object sender, System.EventArgs e)
{
var item = (ObservableItems.Count + 1).ToString();
ObservableItems.Add(item);
photosView.ScrollTo(item, position: ScrollToPosition.MakeVisible, animate: true);
}
}
public分部类主页面:ContentPage
{
ObservableCollection ObservableItems{get;set;}
公共主页()
{
初始化组件();
ObservableItems=新的ObservableCollection(新列表(){“een”、“twee”、“drie”});
PhotoView.ItemsSource=可观察项;
}
void OnScrollMore(System.Object sender,System.EventArgs e)
{
变量项=(observeItems.Count+1).ToString();
增加(项目);
PhotoView.ScrollTo(项目,位置:ScrollToPosition.MakeVisible,动画:true);
}
}
导致:
您的“照片”是否首先有9项,单击按钮后,显示其余按钮?首先检查PhotoView.Y的值是否正确。你可以选择。谢谢@Michael。你的解决方案奏效了。