Mvvm 如何在ViewModel中调用不同的构造函数

Mvvm 如何在ViewModel中调用不同的构造函数,mvvm,dependency-injection,Mvvm,Dependency Injection,在我的ViewModel中,有两种不同的构造函数: public ViewModel() { } public ViewModel(View View) { if (View!=null) { } } 我使用的是RelayCommand,在命令绑定中,我想在单击按钮时调用第二个构造函数。如何调用第二个构造函数以及如何在ViewModel中传递视图。您的ViewModel违反了MVVM模式。View

在我的ViewModel中,有两种不同的构造函数:

    public ViewModel()
    {

    }
    public ViewModel(View View)
    {
        if (View!=null)
        {
        }
    }

我使用的是
RelayCommand
,在命令绑定中,我想在单击按钮时调用第二个构造函数。如何调用第二个构造函数以及如何在ViewModel中传递视图。

您的ViewModel违反了MVVM模式。ViewModel不应该有任何关于具体视图实现的知识。第二,你正在寻找的是依赖注入是的,我正在寻找依赖注入。并在viewmodel中传递视图为什么需要该视图?我想将视图传递到viewmodel中。因为我从Shell创建viewmodel对象,并在viewmodel构造函数中进行初始化_View.DataContext=this;parentWindow=newapplicationWindow(“Title”){WindowContent=\u view,IsResizable=false};因此,我需要将其指定给parentwindow的视图