dependency-injectionmeftemplate10

Template 10 dependency injection using MEF


I'm familiar with using MEF in .NET Framework 4.6.* but not in .NET Core. I'm messing about with the Hamburger template from Template 10 to see if it is suitable for my needs but I haven't been able to figure out how to compose my view models using MEF.

My question is how can I navigate to a view using the navigation service in such a way that its view model will be injected by MEF?


Solution

  • My bad, I hadn't spotted this:

    How do I use a Unity IoC container with Template10?

    In the end, I went for a solution like this:

    public interface IView
    {
        ViewModelBase ViewModel { get; }
    }
    
    [Export]
    public sealed partial class MainPage : Page, IView
    {
        public ViewModelBase ViewModel
        {
            get
            {
                return VM as ViewModelBase;
            }
        }
    
        [Import]
        public MainPageViewModel VM { get; set; }
    
        public MainPage()
        {
            InitializeComponent();
            NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }
    }
    

    And in the 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;
        }