Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement composite Views of composite ViewModels?

Tags:

mvvmcross

I'm just getting into my first project with (the unbelieveably excellent) MVVMCross and I can't figure out how to do something that seems fairly basic: composite views.

Suppose I've got a Person (FirstName, LastName, etc), and a person has an Address (Street, City, PostalCode, etc.). So I'll also have a PersonViewModel and an AddressViewModel.

This is a strictly tablet based app (iPad only, actually) and I want to use containment to have the PersonView contain the (reusable) AddressView, such that the outer (person) view binds to Person, while the inner (address) view binds to Person.Address.

I (dimly) understand the presenter concept for showing the views, as discussed here, but I can't see how to handle the propagation of changes from Person to Address and back.

Suppose the Person object has an Address object, but the PersonViewModel shows the AddressViewModel in an AddressView by passing some sort of Address id and rehydrating. Then the AddressView is binding to a different Address object than the one the Person contains. I don't see how to keep the two in sync, which of course would defeat the whole purpose of binding.

How should I do what I'm trying to do?

like image 604
Joshua Frank Avatar asked Dec 15 '22 09:12

Joshua Frank


1 Answers

This is a really wide topic...

...and there are lots of possible answers.

I think it's important to consider ViewModel's as a very simple concept - I'd like to encourage you to think of ViewModels just as being models for views - definitely don't think of them as 'whole page' objects.

....

Within MvvmCross, you can use ShowViewModel and custom presenters to change the UI if you want to - and this is demonstrated in several MvvmCross examples including the split view presenter - http://slodge.blogspot.com/2013/05/n24-splitviewpresenter-n1-days-of.html

This ShowViewModel technique is really useful for navigation - for changing the whole page or for significant portions of it.

However, you don't have to use navigation paradigms if your app doesn't need them.

If you want to, then you can instead:

  • build your own hierarchies of viewmodels within the core
  • and you can then build your own databound hierarchies of views within the UIs

It's entirely up to you - your app is king.

....

I feel like I'm not explaining this very well...

....

So I gave up and recorded this video - maybe it helps: http://slodge.blogspot.co.uk/2013/06/n32-truth-about-viewmodels-starring.html

The video and sample code only covers the iPad, but I hope it's easy(ish) to see how you would extend it to other platforms:

  • for Windows you can use UserControl
  • for Android you can use MvxFrameControl, custom view or Fragment code.
like image 119
Stuart Avatar answered Mar 16 '23 09:03

Stuart