简体   繁体   中英

SimpleMVVM and Generic ViewModelBase

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.

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. ie

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM