Unity从usercontrol viewmodel到customUsercontrol Viewmodel的依赖注入 [英] Unity Dependency Injection from usercontrol viewmodel to customUsercontrol Viewmodel

查看:29
本文介绍了Unity从usercontrol viewmodel到customUsercontrol Viewmodel的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl 绑定到一个 viewmodel.viewmodel 在构造函数中有参数,这是通过依赖注入完成的.到目前为止,依赖项注入工作正常.

I have a UserControl which is bound to a viewmodel. The viewmodel has parameters in the constructor which is done via dependency injection. The dependency injection is working fine up to this point.

然后我有一个 CustomUserControl,它在我的 UserControl1View 中使用.如何让依赖注入在我的 CustomUserControl 中工作?

And then I have a CustomUserControl which is Used in my UserControl1View. How do I get the Dependency injection working in my CustomUserControl?

我是依赖注入的新手并做了一些研究,但似乎无法让它工作.我在

I am new to dependency injection and did some research, but can't seem to get it working. I get an error on

public partial class UserControl1View : UserControl, IView {
    public UserControl1View( ) {
        InitializeComponent( );
    }
}  

这是错误:

这是代码示例.

用户控件:

public partial class UserControl1View : UserControl, IView {
    public UserControl1View( ) {
        InitializeComponent( );
    }
}

UserControlViewModel:

public class UserControl1ViewModel 
{
    private readonly ISomeDataService dataService;
    public UserControl1ViewModel (ISomeDataService dataservice, IUnityContainer container ) 
    {
       //Please note that the Dependency injection still works in this class, to much to explain the whole structure.   
       this.dataService = dataservice;
       container.RegisterType( typeof( IView ), typeof( CustomUserControlView ) ); 
       var view = container.Resolve<CustomUserControlView>( );
    }

XAML:

<uc:CustomUserControlView/>

<小时>

自定义用户控件:

public partial class CustomUserControlView : UserControl, IView 
{
    public CustomUserControlView(IUnityContainer container) 
    {
        container.RegisterType( typeof( IViewModel ), typeof( CustomControlViewModel ) );
        var viewModel = container.Resolve<CustomControlViewModel>( );
        this.DataContext = viewModel;
        InitializeComponent( );
    }
}

CustomUserControlViewModel:

public partial class CustomUserControlViewModel : UserControl, IView 
{
   private readonly ISomeDataService dataService;
   public CustomUserControlViewModel(ISomeDataService dataservice) 
   {
      var data = dataService.GetsomeCollection()
   }
}

推荐答案

解决方案很简单.不要使用 DI/IoC 容器来注入控件.它不会起作用,也不应该起作用.

Solution is simple. Don't use DI/IoC container to inject controls. It's not going to work and was never supposed to work.

用户控件(与视图相反)是自包含的,可以与其他应用程序或不需要容器的 IDE 设计人员一起开箱即用.否则,它们将无法与设计器很好地合作,因为 Xaml 设计器没有 DI/IoC 的概念,并且不知道如何解析/实例化某个类并且需要无参数构造函数.此外,用户控件"不会将其逻辑拆分为 ViewModel.

User controls (in contrast to views) are meant to be self-contained and work out of the box with other applications or the IDE designer w/o requiring a container. Otherwise they just won't work well with the designer, as the Xaml designer has no concept of DI/IoC and won't know how to resolve/instantiate a certain class and requires an parameterless constructor. Also a "user control" doesn't split it's logic into a ViewModel.

另一面的视图只是一个没有代码的 UI 模板.它也派生自 WindowsUserControl 类,但没有自己的逻辑并且不可重用.视图总是非常特定于特殊的 ViewModel,而 ViewModel 非常特定于一个应用程序.对于视图,可以使用 DI,但只能注入 ViewModel(即取决于您是采用 View-first 还是 ViewModel-first 方法).

A view on other side is just an UI template without code. It also derives from Windows or UserControl class, but has no logic of it's own and is not reusable. A view is always very specific for a special ViewModel and ViewModels are very specific to one application. For views one can use DI, but only to inject the ViewModel (i.e. depending if you are going for View-first or ViewModel-first approach).

附带说明,您的 UserControl1ViewModel 违反了 MVVM,因为您的 VM 引用了您的视图,这违背了 MVVM 的全部目的

On a side note, your UserControl1ViewModel violates MVVM as your VM has references to your View, which beats the whole purpose of MVVM

这篇关于Unity从usercontrol viewmodel到customUsercontrol Viewmodel的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆