【问题标题】:Xamarin Forms ListView Scroll position - MVVMXamarin Forms ListView 滚动位置 - MVVM
【发布时间】:2019-01-25 08:40:37
【问题描述】:

我搜索这篇文章好几个星期了!

更新

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

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

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

【问题讨论】:

  • ListView 默认开启滚动,与 MVVM 模式无关。你有什么问题?
  • 甚至不知道问题是什么,但是 listview 滚动不起作用的唯一原因是如果您将 listview 放在滚动视图中。这两个永远不会在一起
  • @EvZ 感谢您的关注。我已经更新了这个问题。它没有完成。
  • @Luminous_Dev 感谢您的关注。我已经更新了这个问题。没有完成
  • 我为您提供了一个链接,解释了如何以编程方式在ListView 中设置滚动位置。现在,根据您的需要,您可以在 V 层和 VM 层之间创建“桥梁”。它可以通过不同的方式完成,包括您自己已经提到的方式。不幸的是,Stackoverlow 不是编码服务,如果您需要有人帮助,请分享您的代码,您已经尝试过什么,并请描述一个具体问题。目前它只是广泛。

标签: c# listview scroll xamarin.forms


【解决方案1】:

客户视图模型

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

【讨论】:

  • 这不是 MVVM 设计模式。您正在使用代码隐藏。
  • 你对mvvm一无所知。您既不需要事件聚合,也不需要消息传递,也不需要委托! MVVM 并没有说你根本不应该有代码。你可能有一些代码+一些值转换器和动画等。只要你尊重关注点分离,你也很好!当你找到你正在寻找的魔法时告诉我。谢谢
  • @MarzieSagtarash +1 请注意。我要全部!但是我还不能实现事件聚合。如果您对其中任何一个有任何 MVVM 模板,我将不胜感激。我正在努力学习你所说的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 2018-05-15
  • 2018-07-06
  • 1970-01-01
相关资源
最近更新 更多