Dmitry d
Dmitry d

Reputation: 13

How can i use one view for different view models in mvvmcross?

I have many screens that have different business logic, but they are looking similar. I want to create one screen for ios (storyboard or xib), and one MvxViewController, but many different (shared) view models. I cant find a correct way to do it.

Upvotes: 1

Views: 465

Answers (1)

fmaccaroni
fmaccaroni

Reputation: 3916

One way to achieve it is to have a base view with a generic parameter that will be your viewmodel and make as many views as you need inheriting from that one and on each of these views you set the corresponding viewmodel on the generic parameter.

public abstract class MyBaseViewController<TViewModel> :  MvxViewController<TViewModel>
{
     // My view init and logic
}

public class MyView1 : MyBaseViewController<MyViewModel1> {}

public class MyView2 : MyBaseViewController<MyViewModel2> {}

And so on.

Another way a bit more difficult would be to modify your presenter to create a new view with a different viewmodel every time you display a new ViewModel

HIH

Upvotes: 1

Related Questions