Xamarin表格滚动到臀部 [英] Xamarin forms scroll to buttom

查看:77
本文介绍了Xamarin表格滚动到臀部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Xamarin foms应用程序,随着时间的流逝,我会收到消息,我一直希望新消息在传入时显示在ListView的按钮上.

I am creation an Xamarin foms app where I will have messages coming over time, and I always want the new messages to show on the button of my ListView as they are coming in.

此刻,我的页面如下:

 <StackLayout>
   <Button  Text="Login"  />
   <ListView  x:Name="MessageBox" ItemsSource="{Binding TempTest}" ></ListView>
   <Button Command="{Binding AddMessage}" Text="Login"/>
 </StackLayout>

我不知道如何从ViewModel类中滚动,有关如何实现这一点的任何想法?

I cant figure out how to scroll from my ViewModel class, any ideas about how to achieve this?

到目前为止,我能找到的最好的是:

The best that I have been able to find so far are this: http://www.infinite-x.net/2014/10/30/using-the-xamarin-forms-1-3-0-listview-scrollto-method/

但是他现在甚至没有考虑使用MVVM.

But he is not even thinking about using MVVM at this stage.

推荐答案

一种不那么优雅的方法,我想解决的办法是从ViewModel中公开一个Action<Message>,然后您的ContentPage初始化Action并告诉它进行滚动.类似于以下内容(只需用实际的模型名称替换Message即可).

Well the one, less than elegant way, I would know to solve that would be to expose an Action<Message> from your ViewModel and then your ContentPage would initialize that Action and tell it to do the scrolling. Something like the following (just replace Message with what ever the real model name is).

ContentPage:

ContentPage:

public partial class MessagePage : ContentPage {

    private MessageViewModel _viewModel;

    public MessagePage() {
        _viewModel = new MessageViewModel();

        BindingContext = _viewModel;

        _viewModel.OnMessageAdded = message => { //We tell the action to scroll to the passed in object here
            MessageBox.ScrollTo(message, ScrollToPosition.MakeVisible, true);
        }
    }
}

ViewModel:

ViewModel:

public class MessageViewModel {
    public Action<Message> OnMessageAdded { get; set; }

    public ICommand AddMessage { get; protected set; }

    private ObservableCollection<Message> _tempTest;
    public  ObservableCollection<Message> TempTest {
        get { return _tempTest ?? (_tempTest = new ObservableCollection<Message>()); }
        set {
            if(_tempTest != value) {
                _tempTest = value;
                OnPropertyChanged();
            }
        }
    }

    public MessageViewModel() {
        AddMessage = new Command(async () => {
            Message message = SomeClass.GetMessage(); //Get your object from your separate class

            TempTest.Add(message); //Add it to the list that your ListView binds to

            OnMessageAdded?.Invoke(message); //Now run the Action which, if it is not null, your ContentPage should have set to do the scrolling

            //Or if you are not using C#6:
            //Action<Message> onMessageAdded = OnMessageAdded;

            //if(onMessageAdded != null) { onMessageAdded.Invoke(message); }
        });
    }
}

这篇关于Xamarin表格滚动到臀部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆