如何让 Prism/Unity 自动解析视图(用户控件)?

发布于 2024-07-30 00:33:50 字数 1191 浏览 5 评论 0原文

在复合应用程序 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思慕 2024-08-06 00:33:50

您是否已将 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.

梨涡 2024-08-06 00:33:50

有时这实际上不是错误,而是其他原因。 查看内部异常,或调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文