question

Alice-2652 avatar image
0 Votes"
Alice-2652 asked ColeXia-MSFT edited

How not to reload CollectionView after clicking on Back button


Hi I'm facing a problem with my small xamarin app.

I have my small xamarin-form app that everytime the user enter a product Id click OK button retrieves a single (data related to that product Id only) data thru Web API from page A, and then pass it to a collectionview in another page B.

then there is a back button to go back to page A and the user must enter another product Id again and the app must fetch data thru web api again and pass it again to page B in the collectionview.

So the problem is now, when I click on back button in Page B, it's clear the collectionview data, whereas I need the collectionview to keep the state even If I pop back.


I'm not using any MVVM pattern or complicated stuff from now as I'm new to xamarin; it's just a simple architecture.

Or maybe I'm using wrong technics to achieve this and can advise me a better way to achieve that. I'm please open to your suggestion !!!


In brief, I need to be able not to reload CollectionView after clicking on Back button.


Below is the code that Bind to the View on Page B

public async void OnGetProductDetailButton(object sender, EventArgs e)
{

 await Navigation.PushAsync(new ProductDetailPage
 {
     BindingContext = productDetails
 });

}

dotnet-xamarin
· 2
5 |1600 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.

You should post a piece of code where the reload appears

0 Votes 0 ·

I modified the code and added the code where I Navigation.Push to Page B.
If I'm on Page B and I click the built-in Xamarin Back button, it's go back to Page A and it's clear the collectionview...
then when I Navigation.Push again to push B, the collectionview is empty.

0 Votes 0 ·

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ColeXia-MSFT edited

Hello,

Welcome to Microsoft Q&A!

We could create a global variable to store the data from Web API in page A ,and pass the data when pushing to page B .

private ProductDetail productDetails ;

public async void OnGetProductDetailButton(object sender, EventArgs e)
{

    if(productDetails == null){
         //call web api
        // productDetails  = data; 

    }
    await Navigation.PushAsync(new ProductDetailPage
    {
        BindingContext = productDetails
    });
}


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.


5 |1600 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.