In my Shell window I have a workspace region to allow view switching via RequestNavigate. I’m using Unity with a View-First approach such that the view initialization look like:
public partial class WelcomeView : UserControl
{
public WelcomeView(WelcomeViewModel vm)
{
InitializeComponent();
this.DataContext = vm;
}
}
When the application loads, I want to navigate to a default view so my BootStrappers InitializeShell looks as follows:
protected override void InitializeShell()
{
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
regionManager.RequestNavigate("MainWorkspaceRegion", "WelcomeView");
}
Now, my default view, WelcomeView, requires a WelcomeViewModel to be injected in the constructor, and in turn, the WelcomeViewModel requires some service to be injected in its constructor:
public class WelcomeViewModel : INotifyPropertyChanged
{
public WelcomeViewModel(ISomeService someService)
{
// Use some service
someService.SomeEventOccured += new Action(someService_SomeEventOccured);
}
...
}
The problem is when ISomeService is supplied by a module, since the module is not initialized at the time InitializeShell is called, WelcomeViewModel cannot be constructed and in turn neither can WelcomeView.
Whats the proper way to do this?
The proper way to do this is to use a module dependency
So the module that has dependencies will contain
And the module on which it is dependent will contain:
it also makes sense not to use “magic strings”, as I have here and, instead have a static class in your common/infrastructure project that contains the names of all your modules.
This will allow prism to load the modules in the correct order so that your dependencies always resolve.