【问题标题】:Practical example of using Castle.Windsor with MVVMLight Toolkit将 Castle.Windsor 与 MVVMLight Toolkit 一起使用的实际示例
【发布时间】:2011-04-18 22:16:49
【问题描述】:

我确实尝试过,但我找不到将 Castle IOC 与 MVVMLight 结合使用的良好示例。任何形式的动态生成 ViewModelBase 视图模型的指导将不胜感激。我正在尝试使用构造函数注入将视图模型与数据源相关联,例如:

public class MainViewModel : ViewModelBase
{
    ...
    public MainViewModel( ISomeSortofDataRepsoitory mysomesortofdata)
    myrepo = mysomesortofdata;   /// data items in an observable collection
}

我希望ViewModelLocator 执行以下操作:

public static void CreateMain()
{
     if (_main == null)
     {
         ...
         _main = ioc.Resolve<MainViewModel>();
         ...
     }
}

唉,没有骰子。有什么可行的例子吗?

【问题讨论】:

  • 好的,我进行了这种工作,但它不是可混合的。它从容器生成视图模型并根据需要正确注入数据存储库,但容器本身并未在设计模式下实例化,因此没有数据可查看。我必须制作一个虚拟视图模型来布局表单吗?
  • 你能提供一些关于你做了什么的细节吗?我也在用 MvvmLight 和 Windsor 一起玩,只是根本不明白这一点:(

标签: mvvm-light


【解决方案1】:

您需要从 NuGet 安装 CommonServiceLocator.WindsorAdapter 包。然后在你的 ViewModelLocator ctor

var container = new WindsorContainer();
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));

完全删除 ViewModelLocator 类,并将你所有的 Castle Windsor 配置放入

override void OnStartup(StartupEventArgs e)

在 App.xaml.cs 中

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    相关资源
    最近更新 更多