WPF:ViewModel 类型不包含任何可访问的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33337649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: The ViewModel-type does not contain any accessible constructors
提问by mrsargent
I am instantiating my repository in my MainViewModeland passing this instance to use in my child ViewModel(i.e. CategoryViewModel). I am getting a
我正在我的存储库中实例化我的存储库MainViewModel并将此实例传递给我的孩子ViewModel(即CategoryViewModel)。我得到一个
Severity Code Description Project File Line Error The type "CategoryViewModel" does not include any accessible constructors.
严重性代码描述项目文件行错误类型“CategoryViewModel”不包括任何可访问的构造函数。
The error is coming from my main window where I have declared the Category user control and setting the DataContextto CategoryViewModel:
错误来自我的主窗口,我在其中声明了 Category 用户控件并将其设置DataContext为CategoryViewModel:
<view:CategoryView Grid.Row="2" Grid.Column="0" Margin="5">
<view:CategoryView.DataContext>
<viewModel:CategoryViewModel />
</view:CategoryView.DataContext>
</view:CategoryView>
I am not getting this error when I don't have any parameters in the CategoryViewModelconstructor so I know it has something to do with that but not exactly sure what is causing the problem. I would appreciate any advice. Below is my MainViewModeland CategoryViewModel.
当我在CategoryViewModel构造函数中没有任何参数时,我没有收到此错误,所以我知道它与此有关,但不确定是什么导致了问题。我将不胜感激任何建议。下面是我的MainViewModel和CategoryViewModel。
public class MainViewModel : BindableBase
{
private readonly IRepository _repo = new Repository();
private CategoryViewModel _categoryViewModel;
public MainViewModel()
{
_categoryViewModel = new CategoryViewModel(_repo);
}
}
public class CategoryViewModel : BindableBase
{
private IRepository _repo;
public List<Category> CategoryCollection { get; set; }
public CategoryViewModel(IRepository repo)
{
_repo = repo;
CategoryCollection = LoadCategory();
}
private List<Category> LoadCategory()
{
return _repo.GetAllCategories();
}
}
回答by mnistic
You have two options if you want a constructor with parameters: instantiate your view model in code behind, or use ObjectDataProvider class.
如果你想要一个带参数的构造函数,你有两个选择:在后面的代码中实例化你的视图模型,或者使用ObjectDataProvider 类。
回答by Richard June
Here's what I did: In Control.Xaml, I put this in the top level tag(Not sure how to convert it to the full xml notation)
这是我所做的:在Control.Xaml 中,我将其放在顶级标记中(不确定如何将其转换为完整的 xml 表示法)
d:DataContext="{d:DesignInstance Type=visuals:ControlPanelRadPaneVM, IsDesignTimeCreatable=True}"
In Control.Xaml.csI added a constructor accepting the context, marked it as Injection Constructor and assigned dataContext:
在Control.Xaml.cs 中,我添加了一个接受上下文的构造函数,将其标记为注入构造函数并分配了 dataContext:
public partial class Shell : Window
{
[InjectionConstructor]
public Shell(ShellViewModel context)
{
DataContext = context;
InitializeComponent();
}
}
That's it. Now whenever I do a container.Resolve() I get them both. I've not seen any issues yet. but apparently it can be done.
就是这样。现在每当我做一个 container.Resolve() 我都会得到它们。我还没有看到任何问题。但显然可以做到。

