Forum Xamarin.Forms

Changing ContentViews dynamically on a ContentPage with MVVM pattern using Prism

I have been searching for a way of doing this for a time, but I didn't find something clear with what I want to achieve. I'd like to get some hints on which would be the best way to achieve this.

I have a ContentPage with two "containers", on which I would like to alternate 5-6 ContentViews. I was thinking on binding the Content property of these containers to a property in my ViewModel (let's say, an integer), and instantiating the ContentView that I want to show through a converter.

But, I'm kinda new with prism and mvvm and I'm not sure if that would be the best way, and if so, how I could accomplish that. Until now, I had been registering the pages on the 'RegisterTypes' method and using the navigation service. Another thing I want is to re-use the views that have been instantiated, and I don't know if I could do that with the converter way.

Posts

  • JohnHJohnH GBMember ✭✭✭✭✭

    If you need to do that I would bind the IsVisible property of each ContentView with an appropriate property converter to set it on or off depending upon the values in your view model. But it still comes down to your specific use-case.

    Prism is for navigation I believe, so not sure what that has to do with this question.

  • pacomaralpacomaral Member ✭✭

    @JohnH said:
    If you need to do that I would bind the IsVisible property of each ContentView with an appropriate property converter to set it on or off depending upon the values in your view model. But it still comes down to your specific use-case.

    Prism is for navigation I believe, so not sure what that has to do with this question.

    I had thought on that, but by that way, all the contentviews would load their data once the page is opened (if I'm not wrong). The problem is that some of them have extense load of data within a ListView and I guess the performance would be horrible.

    One thing I didn't mention in my first post, is that I'd like the ContentViews to have its own ViewModel. I guess I could use one common ViewModel and have a control on which data is loading depending on the contentview shown, but that's why I was looking for another way.

    I mentioned prism because I have read about 'Partial Views', which consists on having ContentViews with its own ViewModels, but I don't see the way of how to use this within my needs.

  • JohnHJohnH GBMember ✭✭✭✭✭

    Will only 1 contentview be visible at a time? You might consider having specific pages in that case.

  • pacomaralpacomaral Member ✭✭

    @JohnH said:
    Will only 1 contentview be visible at a time? You might consider having specific pages in that case.

    No. I need to have a page with one ContentView at the top, and another one at the bottom. The top side, can show like 3-4 ContentViews which have different ListViews and data. On the bottom, there will be most of the time the same contentview, which will update its data depending on the data selected on the top container.

  • JohnHJohnH GBMember ✭✭✭✭✭

    I get you, so will each contentview contain the same type of data, or be radically different depending upon what is selected in the top container?

  • pacomaralpacomaral Member ✭✭
    edited October 2019

    @JohnH said:
    I get you, so will each contentview contain the same type of data, or be radically different depending upon what is selected in the top container?

    The ContentView of the bottom consists on some entries that will change depending on the selection/actions performed on the top container. Apart from that, in some situations, I would like to show another ContentView (on the bottom), to perform some actions, that differ from the one mentioned above. I could do this on different pages as this is not essentially necessary to do on the same page.

    The main problem is that I do need the top container to show different contentview because the data differs from one to another.

    Sorry for not explaining this all on a single post but my needs are kinda confusing and I don't know how to explain perfectly.

  • JohnHJohnH GBMember ✭✭✭✭✭

    Don't worry you are doing great, and you have highlighted the fact that there really are many ways to do this. The best way is entirely down to you and your understanding of those requirements.
    You could prototype this easily enough, see what works best for you. The loading of any data displayed in each contentview should be asynchronous anyway (the data is in the viewmodel and it does any async fetching of data).

  • pacomaralpacomaral Member ✭✭
    edited October 2019

    If someone is interested, I ended up doing it with a converter.

    So finally I have the page with 2 ContentViews which have the 'Content' property binded to a property in the viewmodel, and with a converter, I instantiate/load (I store them on a List in the page when I create them, so I can re-use the views) the desired view -- which has its own ViewModel.

    For the communication between the viewmodels, I'll probably use the prism's EventAggregator.

Sign In or Register to comment.