Xamarin Forms ListView滚动位置 - MVVM

问题描述 投票:-1回答:1

我好几周都在搜索这篇文章!

UPDATE

是否可以使用MVVM模式控制带有xamarin表单的Listview滚动位置?获取和设置listview的滚动位置。

有一些答案,如实现事件聚合或使用消息或....他们俩都没有为我操作。消息传递并不完全是MVVM模式方式。即使我使用棱镜7,事件聚合也不起作用。没有这么好的示例代码或任何nuget包。

有谁遇到过这个问题并解决了吗?

c# listview scroll xamarin.forms
1个回答
0
投票

CustomersViewModel

public ObservableCollection<Customer> Customers {get;set;}
public Func<Customer,Task> ScrollToCustomer {get;set;}
public DelegateCommand FindBestCustomer {get;set;} // For example    
FindBestCustomer = new DelegateCommand(async() => 
{ 
    Customer bestCustomer = Customers.Last(); // this is our logic in view model!
    await ScrollToCustomer(bestCustomer);
    // at this time, scroll is finished.
});

CustomersView.xaml

<StackLayout>
    <ListView x:Name="CustomersListView" ItemsSource={Binding Customers} />
    <Button Text="Go to last customer" Command="{Binding ScrollToLastCustomer}" /> // For example
</StackLayout>

CustomersView.xaml.cs

override OnBindingContextChanged()
{
    if(BindingContext is CustomersViewModel customersViewModel)
    {
        customersViewModel.ScrollToCustomer = customer => CustomersListView.ScrollTo(customer);
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.