使用MEF的模板10依赖项注入 [英] Template 10 dependency injection using MEF
本文介绍了使用MEF的模板10依赖项注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我熟悉在.NET Framework 4.6.*中使用MEF,但在.NET Core中不熟悉.我正在弄混Template 10中的Hamburger模板,以查看它是否适合我的需求,但是我一直无法弄清楚如何使用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.
我的问题是,如何使用导航服务以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?
推荐答案
我不好,我没发现这个问题:
My bad, I hadn't spotted this:
最后,我寻求了这样的解决方案:
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;
}
}
在App.xaml.cs中:
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;
}
这篇关于使用MEF的模板10依赖项注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文