Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从usercontrol viewmodel到customUsercontrol viewmodel的统一依赖项注入_C#_Wpf_Mvvm_Unity Container - Fatal编程技术网

C# 从usercontrol viewmodel到customUsercontrol viewmodel的统一依赖项注入

C# 从usercontrol viewmodel到customUsercontrol viewmodel的统一依赖项注入,c#,wpf,mvvm,unity-container,C#,Wpf,Mvvm,Unity Container,我有一个UserControl,它绑定到viewmodel。viewmodel在构造函数中有参数,这些参数是通过依赖项注入完成的。到目前为止,依赖项注入工作正常 然后我有一个CustomUserControl,它在我的UserControl1View中使用。 如何使依赖项注入在我的CustomUserControl中工作 我是依赖注入的新手,做了一些研究,但似乎无法让它工作。我收到一个错误 public partial class UserControl1View : UserControl,

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

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

我是依赖注入的新手,做了一些研究,但似乎无法让它工作。我收到一个错误

public partial class UserControl1View : UserControl, IView {
    public UserControl1View( ) {
        InitializeComponent( );
    }
}  
这就是错误:

下面是代码的示例

用户控制

public partial class UserControl1View : UserControl, IView {
    public UserControl1View( ) {
        InitializeComponent( );
    }
}
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>( );
    }
<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( );
    }
}
public partial class CustomUserControlViewModel : UserControl, IView 
{
   private readonly ISomeDataService dataService;
   public CustomUserControlViewModel(ISomeDataService dataservice) 
   {
      var data = dataService.GetsomeCollection()
   }
}
用户控制视图模型

public partial class UserControl1View : UserControl, IView {
    public UserControl1View( ) {
        InitializeComponent( );
    }
}
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>( );
    }
<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( );
    }
}
public partial class CustomUserControlViewModel : UserControl, IView 
{
   private readonly ISomeDataService dataService;
   public CustomUserControlViewModel(ISomeDataService dataservice) 
   {
      var data = dataService.GetsomeCollection()
   }
}

解决办法很简单。不要使用DI/IoC容器注入控件。这是行不通的,本来也不应该管用的

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

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


另一方面,您的
UserControl1ViewModel
违反了MVVM,因为您的VM引用了您的视图,这超出了MVVM的全部目的解决方案很简单。不要使用DI/IoC容器注入控件。这是行不通的,本来也不应该管用的

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

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


另一方面,您的
UserControl1ViewModel
违反了MVVM,因为您的VM引用了您的视图,这超出了MVVM的全部用途

您好,谢谢您的回答,它的帮助超出了您的想象。我想知道以下几点。如果涉及到WPF,视图或用户控件在原则上不是一回事吗?简单地说,我们所有的WPF窗口/视图都是这个特定项目中的用户控件。我有一个名为shared的文件夹,项目中共享的所有usercontrols都位于该文件夹中。因此,我的usercontrol不需要在项目外部使用,因此我想将它链接到一个注入viewmodel的viewmodel。CalendarControl(用户控件)必须绘制日历、句柄单击等,这是代码隐藏的一部分,它公开了特定于应用程序的ViewModel可以绑定到的DependencyProperties。从纯粹的角度来看,没有必要使用DPsHi。谢谢你的回答,它的帮助超出了你的想象。我想知道以下几点。如果涉及到WPF,视图或用户控件在原则上不是一回事吗?简单地说,我们所有的WPF窗口/视图都是这个特定项目中的用户控件。我有一个名为shared的文件夹,项目中共享的所有usercontrols都位于该文件夹中。因此,我的usercontrol不需要在项目外部使用,因此我想将它链接到一个注入viewmodel的viewmodel。CalendarControl(用户控件)必须绘制日历、句柄单击等,这是代码隐藏的一部分,它公开了特定于应用程序的ViewModel可以绑定到的DependencyProperties。从纯角度来看,不需要DPs