YKa
YKa

Reputation: 4104

multiple view models in a single page mvvmcross

I am trying to make an App for multiple platforms using Xamarin. The App uses the Mvvm structure and MvvmCross.

Currently, I have various View Models and they all bind great (using MvvmCross), as long as they are in separate pages.

However, I would like to make a single page that references multiple View Models. For example, a page that has few buttons each binding to methods in different View Models.

I understand that to do this I should:

1) Divide the screen to different views.

2) Assign the buttons to different views according to their View Model.

I am not sure how can I divide the screen? Are fragments the answer?


Also, does this mean a View Model that would know about all view models in the page is necessary?


My current work around is having tabs in all pages that change the current view model of that page, as follow:

public ICommand VM1Command
    {
        get { return new MvxCommand(() => ShowViewModel<ViewModel1>()); }
    }
    public ICommand VM2Command
    {
        get { return new MvxCommand(() => ShowViewModel<ViewModel2>()); }
    }
    public ICommand VM3Command
    {
        get { return new MvxCommand(() => ShowViewModel<ViewModel3>()); }
    }

This is pretty ugly, I would really appreciate if anyone could refer me to somewhere where this is explained or even better tell me how to do it.

Cheers!

Upvotes: 3

Views: 2365

Answers (1)

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

The SpheroViewModel is used in the SpheroView (see: SpheroView.cs). This View is a MvxBindingTabActivityView. So it uses an Android TabHost to display the Subviews as Tab. The setup is done in the lines 30 - 48. For each SubViewModel you have an own view (e.g. SpheroAccelMovementView.cs) that binds and displays the separate data.

For more Details on Tabs and MvvMCross and different Platforms have a look at N = 25 - Video Tutorial. There are perhaps some API changes since stuart has recored the video. But it explains the general idea of tabs and subviewmodels.

Upvotes: 2

Related Questions