1

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)?
raV720
  • 564
  • 3
  • 27

2 Answers2

2

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.

raV720
  • 564
  • 3
  • 27
0

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.

Raven
  • 2,187
  • 7
  • 35
  • 61
  • Thanks for response! Does FreshMvvm support ViewModel first (navigation via ViewModel)? – raV720 Nov 26 '16 at 16:13
  • Yes, FreshMvvM supports ViewModel First navigation. And really, navigations should be via ViewModels. – Raven Nov 27 '16 at 07:57
  • Interesting alternative, wasn't aware of any other mvvm frameworks / toolkits besides Catel that supported nested user controls (with injected models). Catel isn't a good alternative since it doesn't support Xamarin Forms (yet), so you will really need something else for now. Where can I find samples about that feature? Especially the parent/child relations should be interesting for me. – Geert van Horrik Nov 27 '16 at 11:27
  • Thanks Raven, I will try it tomorrow. Could you be more specific about RxUI? You mean ReactiveUI? Does it support nested user controls and ViewModel first navigation? – raV720 Nov 27 '16 at 12:59
  • I have just checked documentation and samples but there is nothing about nesting ViewModels. Could you provide example? – raV720 Nov 28 '16 at 08:18
  • It's very straightforward; you create a BaseViewModel that extends FreshBasePageModel with the [ImplementPropertyChanged] (http://i.imgur.com/L3RbWWv.png) - you put your base functionality for all your viewmodels there. Then now, you create your viewmodels extending that BaseViewModel (http://i.imgur.com/5vKDp41.png) e.g. ProfileViewModel. Note that FreshMvvM is convention over configuration, so when you create a ProfileView.xaml, it automatically knows that your ProfileViewModel.cs is handling that. – Raven Nov 28 '16 at 09:26
  • If you want to create your own Page to PageModel mapper or convention, you can do that by setting the FreshPageModelResolver.PageModelMapper to your custom PageModelMapper. Like this: (Changing setting on App.xaml.cs) http://i.imgur.com/7VVVJRM.png (Code for custom mapper) http://i.imgur.com/sKBI5wB.png – Raven Nov 28 '16 at 09:29
  • You described functionality that is available also in MvvmCross and other frameworks, but what about NESTING ViewModels? – raV720 Nov 28 '16 at 12:38
  • @Raven See https://catelproject.atlassian.net/wiki/display/CTL/Introduction+to+the+nested+user+controls+problem for the real requirements for nested user controls(which is coming to Xamarin.Forms in Catel v5). It's not the same as inheritance. – Geert van Horrik Nov 28 '16 at 16:10
  • @Kokolo Check my answer – raV720 Jul 26 '17 at 06:17