MEF 和 MVVM - 添加约定的方法?
有没有办法连接到 CompositionContainer 的内部?
例如,假设任何时候在以“View”一词结尾的导出上调用导入时,我可能希望将以“ViewModel”结尾的导出注入到其 DataContext 中。
有办法做到这一点吗?我试图在每个视图中避免这样的代码:
[Export("MainView")]
public partial class MainView : Window
{
[ImportingConstructor]
public MainView([Import("MainViewModel")] dynamic viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
}
最好简化为这样,并使用约定:
[Export("MainView")]
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
}
}
Is there a way to hook into the internals of CompositionContainer?
For example, let's say any time an import is called on a export ending with the word "View" I might want to inject an export ending with "ViewModel" into its DataContext.
Is there a way to do that? I'm trying to avoid code like this in every View:
[Export("MainView")]
public partial class MainView : Window
{
[ImportingConstructor]
public MainView([Import("MainViewModel")] dynamic viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
}
Would be nice to simplify to just this, and use conventions:
[Export("MainView")]
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一些人正在研究这个问题,显然包括 Mef 团队:)
这是您可能会感兴趣的博客文章:
http://thecodejunkie.com /2010/03/bringing-convention-based-registration.html
这是另一个稍微不太详细的:
http://blogs.msdn.com /b/hammett/archive/2011/03/08/mef-s-convention-model.aspx
There are some folks working on this, including the Mef team apparently :)
Here is a blog post you might find interesting:
http://thecodejunkie.com/2010/03/bringing-convention-based-registration.html
And here is another slightly less detailed one:
http://blogs.msdn.com/b/hammett/archive/2011/03/08/mef-s-convention-model.aspx
我猜您正在寻找一个可以减少您工作量的库或框架。
请查看库WPF 应用程序框架。我用得很好。
I Guess you are looking for a library or framework which can reduce your effort.
Please have a look at library WPF Application framework. I am using it very well.