【发布时间】:2011-06-03 16:50:59
【问题描述】:
我真的很喜欢通过 XAML 在视图的 DataContext 上声明我的 ViewModel 的灵活性,但我很难弄清楚如何将此 ViewModel 绑定到系统的其余部分。
例如
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ViewModels">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
这里的问题是,我的 ViewModel 经常与系统的其他部分具有并共享依赖关系,我不确定如何将另一个对象的引用注入到上面声明的 MainViewModel 中。
如果我正在执行手动依赖注入,那么我会在应用程序开始时创建一堆工厂,负责将所有内容连接起来。
如何在 MVVM WPF 应用程序中采用相同的方法?处理我的所有 ViewModel 的最有效方法是什么?
【问题讨论】:
标签: wpf xaml dependency-injection datacontext bootstrapping