获取列表视图滚动到以 xamarin 窗体 wpf 结尾的起始位置



我一直致力于让列表视图滚动位置滚动到以 xamarin 表单 WPF 应用程序结尾。我已经尝试了以下解决方案,它适用于ios和Android,但不幸的是,它不适用于wpf应用程序。请提出任何想法,以便在 xamarininform WPF 应用程序中获取列表视图结束的滚动位置。

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

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

如果使用 Xamarin 窗体,则可以创建从 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();
    }
}

最新更新