如何让 Prism/Unity 自动解析视图(用户控件)?
在复合应用程序 (Prism) 中,当我的模块加载时,我收到此错误:
{"当前构建操作(构建 关键构建 键[CustomersModul.ViewModels.CustomerAllViewModel, null]) failed: 参数视图 尝试时无法解决 调用构造函数 CustomersModul.ViewModels.CustomerAllViewModel(CustomersModul.Views.CustomerAllView 看法)。 (策略类型 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, 索引 2)"}
我解析这个类:
CustomerAllViewModel layoutManager = this.container.Resolve<CustomerAllViewModel>();
该类看起来像这个:
public class CustomerAllViewModel : ViewModelBase
{
public CustomerAllView View { get; set; }
public ICommand TextChangeCommand { get; set; }
private Customer customer;
public CustomerAllViewModel(CustomerAllView view)
{
View = view;
view.DataContext = this;
...
通常我解析没有构造函数参数的Presenters并实例化这是我第一次使用接受 View 作为参数的 ViewModel
有趣的是,当我使用 Resharper 访问视图时,它会询问我是否。我想要转到 XAML 或隐藏代码,所以 Prism 可能对实例化哪一个感到困惑?
如何让 Prism 在参数中自动实例化此视图(具有 XAML 和代码隐藏的 UserControl)?
In a Composite Application (Prism), when my module loads, I get this error:
{"The current build operation (build
key Build
Key[CustomersModul.ViewModels.CustomerAllViewModel,
null]) failed: The parameter view
could not be resolved when attempting
to call constructor
CustomersModul.ViewModels.CustomerAllViewModel(CustomersModul.Views.CustomerAllView
view). (Strategy type
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy,
index 2)"}
I am resolving this class:
CustomerAllViewModel layoutManager = this.container.Resolve<CustomerAllViewModel>();
And that class looks like this:
public class CustomerAllViewModel : ViewModelBase
{
public CustomerAllView View { get; set; }
public ICommand TextChangeCommand { get; set; }
private Customer customer;
public CustomerAllViewModel(CustomerAllView view)
{
View = view;
view.DataContext = this;
...
Normally I resolve Presenters which have no constructor parameters and instantiate their views internally. This is the first time I am using a ViewModel which accepts a View as parameter.
Interestingly, when I go to the view with Resharper, it asks me if I want to go to the XAML or code behind, so perhaps Prism is getting confused which one to instantiate?
How can I get Prism to automatically instantiate this view (UserControl with XAML and code-behind) in the parameter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已将 CustomerAllView 添加到容器中? 当我忘记向容器添加类型时,我通常会收到此错误。
Have you added the CustomerAllView to the container? I usually get this error when I have forgot to add a type to the container.
有时这实际上不是错误,而是其他原因。 查看内部异常,或调用 Microsoft.Practices.Composite.ExceptionExtensions.GetRootException(ex) 来获取根异常。 您可能会发现您没有看到的构造函数之一抛出了一些错误。
Sometimes this is actually not the error, but something else. Look at the inner exceptions, or call Microsoft.Practices.Composite.ExceptionExtensions.GetRootException(ex) to get the root exception. You'll likely find there is some error being thrown in one of your constructors you are not seeing.