I have a Template10 based application with a modal dialog for a login screen as shown in the example at the Template10 GitHub page.
https://github.com/Windows-XAML/Template10/tree/master/Samples/Search
I would like to resolve a login view model using MEF. I'm doing dependency injection as described in the answer to this question.
Template 10 depency injection using MEF
Here's an extract of my App.xaml.cs.
public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
var config = new ContainerConfiguration();
_container = config.WithAssembly(GetType().GetTypeInfo().Assembly).CreateContainer();
await NavigationService.NavigateAsync(typeof(Views.MainPage));
}
public override INavigable ResolveForPage(Page page, NavigationService navigationService)
{
_container.SatisfyImports(page);
return (page as IView)?.ViewModel;
}
That works fine for views that I navigate to using the navigation service but not my login view. Here's the modal dialog in shell.xaml.
<Controls:ModalDialog x:Name="LoginModal"
CanBackButtonDismiss="False"
DisableBackButtonWhenModal="True">
<Controls:ModalDialog.ModalContent>
<views:LoginView x:Name="loginPart"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HideRequested="LoginHide"
LoggedIn="LoginLoggedIn" />
</Controls:ModalDialog.ModalContent>
</Controls:ModalDialog>
In my login view's code behind, I have this property that I would like to resolve but it's always null.
[Import]
public LoginViewModel VM { get; set; }
Is there some way to resolve the dependencies of a modal dialog?
Update
I've just noticed that the login view gets constructed before App.OnStartAsync is called. I moved the configuration of the IOC container into App.CreateRootElement so that it's done before the login view constructor is hit but the problem remains.
Update
A messy partial workaround is to have a static class with a reference to the IOC container and call it from the view's constructor.
public LoginView()
{
this.InitializeComponent();
IOCContainer.Container.SatisfyImports(this);
}
I would definitely like to avoid relying on a static class like this. However even doing this, the view model's navigation service is null.