How can I jump from one veiwmodel to another viewmodel by passing parameter on item tapped ?

h_kumarh_kumar Member ✭✭
edited July 26 in Xamarin.Forms
I want to navigate page from category page(CategoryViewModel) to product page.(ProductViewModel)... How can I pass parameter on listviewitemtapped and display the data of particular catID on product page...
Plz if anyone can provide me the solution..
Thanks I'm new to xamarin

Answers

  • FaizalSaidaliFaizalSaidali USMember ✭✭✭

    @h_kumar, I suggest to you for Prism. That will helps you to navigation and data passing keep simple
    https://prismlibrary.github.io/docs/xamarin-forms/Getting-Started.html

  • GraverobberGraverobber Member ✭✭✭

    You didn't share any code so it is hard to assume.

    Define a constructor in your Productpage that either takes the catId or the whole CategoryViewModel as parameter.
    Then ListviewItemTapped should be an event in your Categorypage.
    When it happens you do something like this:

    CategoryViewModel tappedModel = sender as CategoryViewModel;
    ProductPage prodPage = new ProductPage(tappedModel.catId);
    await Navigation.PushAsync(prodPage);
    
  • FaizalSaidaliFaizalSaidali USMember ✭✭✭

    @Graverobber said:
    You didn't share any code so it is hard to assume.

    Define a constructor in your Productpage that either takes the catId or the whole CategoryViewModel as parameter.
    Then ListviewItemTapped should be an event in your Categorypage.
    When it happens you do something like this:

    CategoryViewModel tappedModel = sender as CategoryViewModel;
    ProductPage prodPage = new ProductPage(tappedModel.catId);
    await Navigation.PushAsync(prodPage);
    

    Hi @Graverobber, I think he can resolve the blockage by your recommended way, but Xamarin has an standard. Please make sure your code in MVVM architecture. Keep separately maintain the UI layer and logic layer. Otherwise it will complicate to handle after a long journey.

    I think @h_kumar on beginning stage on his project. So he can easily adopt a architecture. That's why I'm preferring to use Prism architecture.

  • GraverobberGraverobber Member ✭✭✭

    @FaizalSaidali
    Well of course the code I shared has to be adapted accordingly into MVVM.
    But switching the pages (views) is a UI task and should be done in the ContentPage. Therefore the tap is handled in the tap event in the ContentPage. The passed parameter can then be used to initialise the ViewModel for the Productpage. Or directly pass a Product Viewmodel to the constructor. I don't see that it violates MVVM.
    I don't know prism, I have to see what it is and what it does.

  • ColeXColeX Member, Xamarin Team Xamurai
  • h_kumarh_kumar Member ✭✭

    thanks @FaisalSaidali thanks for yr suggestion i m looking at Prism Navigation as well

  • h_kumarh_kumar Member ✭✭
    edited July 28

    hi @Graverobber

    i m fetching data from sql server using webapi service.
    getting the list of categories Using get method - GetCategories. then i have created CategoryViewModel to retrieve data for GetCategories using json converter it displays me the list of categories.

    now i aslo have created get method - GetProductByCatID(int Catid)
    what the query i used in webapi is "select * from tblProductMas where Catid="+Catid
    and created ProductbyCatIDViewModel to retrieve the data for GetProductbyCatID.

    @ColeX exactly the same thing i want to know how to pass the parameter (Catid from GetCategories = Catid from GetProductByCatID) on listviewitemTapped at the time of navigation.

    @Graverobber if u can tell me which code i should share u? i mean codding of CategoryViewModel, ProductbyCatIDViewModel or code behind of xamlpage ?

  • JohnHJohnH GBMember ✭✭✭✭✭

    @Graverobber said:
    @FaizalSaidali
    Well of course the code I shared has to be adapted accordingly into MVVM.
    But switching the pages (views) is a UI task and should be done in the ContentPage. Therefore the tap is handled in the tap event in the ContentPage. The passed parameter can then be used to initialise the ViewModel for the Productpage. Or directly pass a Product Viewmodel to the constructor. I don't see that it violates MVVM.
    I don't know prism, I have to see what it is and what it does.

    Alternatively, imo a cleaner approach is that the tap is handled in the view model via a command, the view model fetches the next page via DI and pushes it onto the Navigator stack. Entirely mockable, entirely testable.

Sign In or Register to comment.