获取listview滚动的开始位置以xamarin形式WPF结尾

时间:2019-01-31 04:47:29

标签: xamarin xamarin.forms xamarin.windows

我一直在努力使listview滚动位置滚动到xamarin表单WPF应用程序中。我试过下面的解决方案,它可以在ios和android中工作,但是不幸的是,它不能在wpf应用程序中工作。请提出任何想法在xamarinforms WPF应用程序中获取列表视图结尾的滚动位置。

您可以在下面的链接中找到示例代码

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

1 个答案:

答案 0 :(得分:0)

如果您使用的是Xamarin Forms,则可以创建一个从ListView扩展的控件,并添加滚动到顶部或底部的方法。

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());
            }
        }
    }
}

在您的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>

在后面的代码中,您可以执行以下操作:

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

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

        this.customListView.ScrollToLast();
    }
}