Repository navigation
Replies: 3 comments 17 replies
|
I normally use a separate navigation controller for this. The NavigationController has a reference to the ShellViewModel, and tells it to display particular pages. There's a often dependency cycle there, so I explicitly give the NavigationController a reference to the ShellViewModel in the Bootstrapper, after it's been constructed. I put together a full working sample here: https://github.com/canton7/Stylet/tree/develop/Samples/Stylet.Samples.NavigationController . Let me know if you've got any questions! |
|
So, a few questions with the example you listed.
public NavigationController(Func<Page1ViewModel> page1ViewModelFactory, Func<Page2ViewModel> page2ViewModelFactory)
{
this.page1ViewModelFactory = page1ViewModelFactory ?? throw new ArgumentNullException(nameof(page1ViewModelFactory));
this.page2ViewModelFactory = page2ViewModelFactory ?? throw new ArgumentNullException(nameof(page2ViewModelFactory));
}Or would something like this work, since I have a fair amount of ViewModels that I might need to navigate to? private readonly IContainer _container;
public NavigationController(IContainer container)
{
_container = container
}
public void NavigateToPage1()
{
var pg1 = _container.Get<Func<Page1ViewModel>>();
this.Delegate?.NavigateTo(pg1);
}
public ModifyGameHostViewModel(IContainer container)
{
_container = container;
var gamePath = _container.Get<Func<ModifyGamePathViewModel>>().Invoke();
ActivateItem(gamePath);
} |
Correct. In that case you could let the ioc container inject the ShellViewModel directly into the NavigationController, without needing to explicitly assign it later in the bootstrapper.
You can inject the IContainer, sure. That's the Service Locator pattern: it's often seen as an anti-pattern, but IMO it's fine for something like the NavigationController (although I wouldn't do it in a normal ViewModel). Note that there's no need to fetch a var pg1 = _container.Get<Page1ViewModel>(); |
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to figure out what is the best way to be able to change the active item of a different viewmodel than the control I'm currently on, without having to resort to singletons/static instances. For example, something like this:
. Then I could click the button, and have it switch over to something like this:
.
The Red is the ShellViewModel/RootViewModel.
I'm trying to get rid of the "Instance" static instance. I'm also looking for a way where I could call something like ActivateSettingsClick(), to activate the settings ViewModel.
All reactions