1

我一直致力于让 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
4

1 回答 1

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();
    }
}
于 2019-01-31T05:14:07.210 回答