Automatic scroll using mvvm

KhosrouKhosrou Member ✭✭

Hi folks,

I've implemented a simple chat UI in my app, and when I open the chat I want automatic scroll so it goes to the last item/cell of the listView. I tried MessageCenter and all kind of things but no result. Can someone help me out how to correctly implement auto scroll to last item functionality in a ListView using mvvm?

Merci!

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Its a ListView? You can use a Behavior for that. Just make it so the ListView always scrolls to the bottom when new elements are added to the collection.

  • KhosrouKhosrou Member ✭✭

    How do I that? Do you have an example? I'm an amature at behaviors ;(

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/behaviors/

    I'll give you some hints but I think one learns more by at least trying first. If you struggle on this for more than a day, come back with the code you've written and we'll get you back on track.

    In my behavior for this same thing:
    I override when the the BindingContext is changed. That way I can subscribe to the CollectionChanged event of the AssociateObject.Source {see the hints here? :wink: }
    protected override void OnBindingContextChanged()

    Then in your handler for that event
    private void ListViewBehaviors_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    Get the collection
    Get the item that is the .Last item of the collection
    Then on the ListView that is the AssociatedObject use the .ScrollTo() method to ... well.. Scroll To that Item.

  • KhosrouKhosrou Member ✭✭

    Great, thanks for the hints! I will work on it.

  • KhosrouKhosrou Member ✭✭

    by the way I fixed this!

  • NMackayNMackay GBInsider, University mod
    edited July 2018

    @Khosrou said:
    by the way I fixed this!

    Repo and post your fix for others, saying "by the way I fixed this!" helps no one especially after others have tried to assist you if you don't share, saying that without any example is helpful to no one, a github a sample etc.

  • KhosrouKhosrou Member ✭✭
    I always share if I fix something. I wasnt finish here because I fixed the issue but I don’t know yet how I did it. I was so enthusiastic that i post it. Im now planning to share how I did it as soon as I understand it first by my self! I did it without a behavior tho.
Sign In or Register to comment.