0

In Xamarin Forms i have a ListView and the following method in Code Behind:

    protected async override void OnAppearing()
    {
        base.OnAppearing();

        events.ItemsSource = await App.ServiceManager.GetStream();
    }

where

events

is my ListView and fetch data from rest webservice.

When i select an item in the ListView i push a detail page. The problem is that when i pop back to the ListView, the method OnAppearing() is called and make the remote call again.

Instead i'd like that the scroll start from the previous position (before that i push a new page): how can do that?

1
  • Move the logic to ViewModel and create a single instance of it - for example. Commented Mar 8, 2017 at 19:15

2 Answers 2

1

I think you can simply use a

bool isFirstAppearing = true;

protected async override void OnAppearing()
{
    base.OnAppearing();

    if(isFirstAppearing) {
         isFirstAppearing = false;
         events.ItemsSource = await App.ServiceManager.GetStream();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This does not work if you have a command to go back to that page. @Alessandro Caliaro
0

You can either do a check if its already been loaded, as alessandro Caliaros answer suggests, or an alternative if you want to get fresh data but still be scrolled in the same position, you can add code in onappearing to set the scroll position to be the same as it was before:

var currentYPosition = _scrollView.ScrollY;
await _scrollView.ScrollToAsync(_scrollView.ScrollX, currentYPosition, true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.