我正在尝试使用 MEF 和 MVVMLight 来构建我的应用程序。
我已经设法将一些东西连接在一起,现在可以正常工作并成功导入,但在此过程中,我似乎完全错过了 ViewModelLocator,我只是想知道你如何正确使用 MEF 和 ViewModelLocator,也许你真的需要一个,或者是否我的设计出了问题?
因此,在我的 App.xaml 中,我禁用了 startupUri,在 App.xaml.cs 中我这样做:
[Import("MainWindow", typeof(Window))]
public new Window MainWindow
{
get { return base.MainWindow; }
set { base.MainWindow = value; }
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Load catalog in normal way
...
MainWindow.Show();
}
我的 MainWindow 代码是这样的:
[Export("MainWindow", typeof(Window))]
public partial class MainWindow : Window
{
[ImportingConstructor]
public MainWindow([Import("MainViewModel")] MainViewModel vm)
{
InitializeComponent();
DataContext = vm;
}
}
最后我的 ViewModel 是这样的:
[Export("MainViewModel", typeof(MainViewModel))]
public class MainViewModel : ViewModelBase, IPartImportsSatisfiedNotification
{
// I do some MEF imports here also
}
但是我以正确的方式做这件事还是有更明智的方法?我真的可以忽略 ViewModelLocator 吗?