C#C
C#3y ago
Indeed

❔ Class with parameter as a generic parameter

Hi!
I'm trying to write a navigation system in wpf

My setup rn using default constructor is

public class ViewModelBase : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public virtual void Dispose() { }
}


public class NavigationService<TViewModel> where TViewModel : ViewModelBase {
    private readonly NavigationStore _navigationStore;
    private readonly Func<TViewModel> _createViewModel;

    public NavigationService(NavigationStore navigationStore, Func<TViewModel> createViewModel) {
        _navigationStore = navigationStore;
        _createViewModel = createViewModel;
    }

    public void Navigate() {
        _navigationStore.CurrentViewModel = _createViewModel();
    }
}


however i want to change it so each ViewModelBase has a public custom constructor like TViewModel.NavigateTo() that returns its instance to be able to define some common logic between navigables

I've tried to do
public interface Navigable<TViewModel> where TViewModel : ViewModelBase {
    TViewModel NavigatedTo();
}


public class NavigationService<Navigable<TViewModel>> where TViewModel : ViewModelBase {

but it breaks
Was this page helpful?