C
C#2y 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 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();
}
}
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 interface Navigable<TViewModel> where TViewModel : ViewModelBase {
TViewModel NavigatedTo();
}
public class NavigationService<Navigable<TViewModel>> where TViewModel : ViewModelBase {
public class NavigationService<Navigable<TViewModel>> where TViewModel : ViewModelBase {
but it breaks
7 Replies
Sossenbinder
Sossenbinder2y ago
How does it break? Are you attempting to call .NavigatedTo() within your NavigationService?
cathei
cathei2y ago
Is there reason you can't do this?
public class NavigationService<TViewModel> where TViewModel : ViewModelBase
{
public NavigationService(NavigationStore navigationStore, INavigable<TViewModel> navigable) { /* ... */ }
}
public class NavigationService<TViewModel> where TViewModel : ViewModelBase
{
public NavigationService(NavigationStore navigationStore, INavigable<TViewModel> navigable) { /* ... */ }
}
Indeed
IndeedOP2y ago
In the typing itself
cathei
cathei2y ago
Do you want this?
public class NavigationService<TViewModel>
where TViewModel : ViewModelBase, INavigable<TViewModel>
public class NavigationService<TViewModel>
where TViewModel : ViewModelBase, INavigable<TViewModel>
Indeed
IndeedOP2y ago
gimme a sec to follow this train of thought in ym code oh, seems right Yes! It works thank you! ❤️
cathei
cathei2y ago
Yay cathype
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server