question

BananaSupreme avatar image
0 Votes"
BananaSupreme asked ·

Xamarin Forms CollectionView ScrollTo when called externally

I have this control that is mainly a collectionview and occasionally I have to use changes elsewhere to scroll the collectionview, so I exposed a ScrollTo method which I am triggering when an event in another control is called.

I only tested on android.

I followed breakpoints and ScrollTo is being called but no scrolling happens, (scrolling actually doesnt work also when the page is loaded, or when on appearing calls it in the page that implements the control, for the first one I saw the response, and since its a contentview and not contentpage I dont have OnAppearing to use) I have an OnScrolled Event Attached that isn't called as well.

I am also sure it is meant to scroll as the index I am calling is not the one I have;

Exposed ScrollTo

 public void ScrollToOffset(int Offset, bool animate = true)
         {
             int index = currentIndex + Offset;
             collectionView.ScrollTo(index: index, animate: animate);
         }

Calling Code

 public void OnEvent(object sender, EventArgs e)
         {
             Control.ScrollToOffset(1);
         }


I verified it is calling on UI thread by using Device.BeginInvokeOnMainThread() and trying to Focus the objects doesn't seem to help.
The call I placed in the OnAppearing does seem to work when is called by HotReload but the one called by the event still doesn't work.

dotnet-xamarinforms
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
1 Vote"
ColeXia-MSFT answered ·



Hello,

Welcome to Microsoft Q&A!

You can call the method in SizeChanged event .

bool isFirstLoading = true;
        public YourPage()
        {
            InitializeComponent();
            BindingContext = new MonkeysViewModel();

            this.SizeChanged += (sender,e)=> {
                if (isFirstLoading)
                {
                    collectionView.ScrollTo(index: 10);
                    isFirstLoading = false;
                }
            };
        }



If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 6 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the welcome!

Thanks for the answer aswell, it works for starting up the scroll position perfectly!

Would you know anything about calling the scroll through an event when the item is not the focus,

for example I have the collectionview as part of a contentview that is than defined in a page.
I have also defined an event elsewhere in the page that when it happens the collectionview needs to scroll, and that is not responding

Thank you again!

0 Votes 0 ·

Did you try to call collectionView.Focus() first ?

0 Votes 0 ·

Yes, I also tried it in different points of the code, it seems that whenever I try to control the code externally from when the collection is directly controlled and focused it doesn't work...

0 Votes 0 ·
Show more comments