简体   繁体   中英

Get start position of a listview scroll to end in xamarin forms wpf

I have worked on getting listview scroll position scroll to end in xamarin forms WPF application. I have tried below solution, it works in ios and android but unfortunately, it doesn't work in wpf application. Please suggest any idea to get scroll position of a listview end in xamarinforms WPF application.

Sample code you can find in below link

https://stackoverflow.com/questions/40373761/how-to-set-listview-to-start-showing-the-last-item-instead-in-xamarin-forms

If you're working with Xamarin Forms, you can create a control that extend from ListView and add methods for scrolling to top or bottom.

namespace YourAppName.Controls
{
    public class CustomListView : ListView
    {
        public CustomListView() : this(ListViewCachingStrategy.RecycleElement)
        {
        }

        public CustomListView(ListViewCachingStrategy cachingStrategy)
            : base(cachingStrategy)
        {
        }

        public void ScrollToFirst()
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                try
                {
                    if (ItemsSource != null && ItemsSource.Cast<object>().Count() > 0)
                    {
                        var firstItem = ItemsSource.Cast<object>().FirstOrDefault();
                        if (firstItem != null)
                        {
                            ScrollTo(firstItem, ScrollToPosition.Start, false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
            });
        }

        public void ScrollToLast()
        {
            try
            {
                if (ItemsSource != null && ItemsSource.Cast<object>().Count() > 0)
                {
                    var lastItem = ItemsSource.Cast<object>().LastOrDefault();
                    if (lastItem != null)
                    {
                        ScrollTo(lastItem, ScrollToPosition.End, false);
                    }
                }
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
    }
}

And on your xaml:

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:YourAppName.Controls"
    x:Class="YourAppName.Views.CustomListViewPage">
    <controls:CustomListView
        x:Name="customListView"
        ItemsSource="{Binding Items}"
        SeparatorVisibility="None"
        SelectionMode="None"
        HasUnevenRows="true">
        <controls:CustomListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label
                            FontSize="Medium"
                            Text="{Binding TestText}" />
                    </ViewCell>
                </DataTemplate>
            </controls:CustomListView.ItemTemplate>
    </controls:CustomListView>
</ContentPage>

And on the code behind you can do something like this:

namespace YourAppName.Views
public partial class CustomListViewPage : ContentPage
{
    public CustomListViewPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        this.customListView.ScrollToLast();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM