1
votes

I am looking for a solution/mvvm framework that supports nesting ViewModels and Views. What I mean is:

  1. Each ViewModel derives from BaseViewModel
  2. ViewModels have properties that are of type BaseViewModel which are sub-ViewModels (nested inside parent ViewModel)
  3. Each ViewModel have corresponding View
  4. Views have ContentControl (control that can display templated view) corresponding to sub-ViewModels of corresponding ViewModel
  5. Now, when creating instance of ViewModel it is needed to pass appropriate instances of concrete sub-ViewModels. Views should be automatically resolved and nested (somehow) base on ViewModels structure.

I do not define somehow because there may be a lot of ways to do it.

I hope my idea is clear. This approach allows easy and dynamic creation of ViewModels and Views. Just create tree of ViewModels, for example in XML, and base on this create new functionality.

The questions are:

  1. Is there any mvvm framework (mvvmcross, catel) supporting such approach for Xamarin.Forms?
  2. How would you store tree of ViewModels - in XML, database tables, ...?
  3. How would you create instances of ViewModels - deserialization, dependency injection, ...?
  4. How to create Views and resolve (if framework does not support it)?
2

2 Answers

2
votes

After some time I can share some experience about the questions I asked:

  1. I do not know whether there is any mvvm framework supporting such approach. Probably Catel v5 is going to support this, but I did not checked this. I use custom solution.
  2. In my solution I store ViewModels definitions in single database table in parent/child structure.
  3. ViewModel instances are created by custom factories using definitions from database table.
  4. Views are created using ValueConverters. It is possible, because each view has bindings created base on ViewModels structure.

Beside above answers I can suggest to use Prism. Although it has some disadvantages for me it is the best framework in such approach.

0
votes

Yes! There's an MVVM Framework that fits exactly to what you are looking for and is created with Xamarin.Forms in mind:

FreshMvvM: https://github.com/rid00z/FreshMvvm

Quickstart Guide: http://www.michaelridland.com/xamarin/freshmvvm-quick-start-guide/

How does it compare to other options?

It's super light and super simple
It's specifically designed for Xamarin.Forms
Designed to be easy to learn and develop (great when you are not ready for RxUI)
Uses a Convention over Configuration

Features

PageModel to PageModel Navigation
Automatic wiring of BindingContext
Automatic wiring of Page events (eg. appearing)
Basic methods (with values) on PageModel (init, reverseinit)
Built in IOC Container
PageModel Constructor Injection
Basic methods available in Model, like Alert
Built in Navigation types for SimpleNavigation, Tabbed and MasterDetail

You can nest or derive ViewModels as much as you'd like (In our case we have a BaseViewModel). We have been using FreshMvvM for our startup and has been battle tested to work and fit to whatever we need.