SimpleMVVM and Generic ViewModelBase

244 views Asked by At

I recently found the SimpleMVVM toolkit and am trying to create a small example program. I am trying to create a CurrentViewModel parameter like so:

    private ViewModelBase<>  _CurrentViewModel;
    public ViewModelBase<>  CurrentViewModel
    {
        get { return _CurrentViewModel; }
        set
        {
            _CurrentViewModel= value;
            NotifyPropertyChanged(m => m.CurrentViewModel);
        }
    }

Any object referenced by CurrentViewModel will extend the SimpleMVVM ViewModelBase class like so:

public class HomeViewModel : ViewModelBase<HomeViewModel>
{ }

The problem I am having is that SimpleMVVM ViewModelBase requires a type T as an argument and I don't know how to create the parameter CurrentViewModel such that it can accept any ViewModel extending ViewModelBase.

1

There are 1 answers

0
kidshaw On

One of the issues around using Generics '<T>' is that any consumer has to still know the type. If you consider adding an ICollection to your model, you have to know what it is a collection of so that you maintain strong typing.

The only exception is if you define a class which is itself generic, which can then pass on it#s type property to a child class. i.e.

CustomCollection<T>
{
     ICollection<T> _foo;
}

To do what you're trying to do will require a seperate common interface that encapsulates the functionality you want from CommonViewModel.