使用Catel与Orc.Wizard创建向导时报视图模型无法解析错误
问题修复要点
你遇到的 The view model of the view 'QuotationWizardPageView' could not be resolved 错误由语法错误、定位规则配置不全、命名空间不匹配三类问题共同导致,具体遗漏和错误点如下:
- 主向导类存在基础语法错误
你贴出的ASWizard类代码存在多处语法问题,会直接导致类型无法被正确识别:- 命名空间声明后直接写继承关系是非法C#语法,继承关系必须定义在类上;
- 类声明缺失
class关键字; - 向导标题属性名拼写错误,正确属性名为
Title,你写为Tittle; - 部分using语句末尾缺失分号,Catel IoC的正确命名空间为
Catel.IoC(末尾C大写),你写为Catel.Ioc。
错误原代码:
修正后代码:using Catel.Ioc; using Orc.Wizard; namespace ZMAdmin.ASWizard : WizardBase { public ASWizard(ITypeFactory typeFactory) : base (typeFactory) { Tittle = "Bedrijfsnaam"; this.AddPage<QuotationWizardPage>(); } }using Catel.IoC; using Orc.Wizard; using System; namespace ZMAdmin.ASWizard { public class ASWizard : WizardBase { public ASWizard(ITypeFactory typeFactory) : base (typeFactory) { Title = "Bedrijfsnaam"; AddPage<QuotationWizardPage>(); } } } - 视图-视图模型只注册了单向映射,不符合Catel定位要求
Catel加载视图时,需要通过IViewModelLocator从视图类型反向查找对应视图模型,你只注册了视图模型 -> 视图的单向映射,缺失反向映射配置,导致加载视图时找不到对应VM。
原app.xaml.cs中的注册代码需要补全反向注册:
注意使用var viewLocator = ServiceLocator.Default.ResolveType<IViewLocator>(); viewLocator.Register(typeof(QuotationWizardPageViewModel), typeof(QuotationWizardPageView)); // 补全视图到视图模型的反向映射 var viewModelLocator = ServiceLocator.Default.ResolveType<IViewModelLocator>(); viewModelLocator.Register(typeof(QuotationWizardPageView), typeof(QuotationWizardPageViewModel));IViewModelLocator需要提前引用Catel.MVVM命名空间。 - 视图后台代码未正确实现
你只贴出了页面的XAML代码,没有提供对应后台代码,QuotationWizardPageView的后台类必须满足两个要求:- 继承Catel提供的
UserControl基类(位于Catel.Windows.Controls命名空间),而非WPF原生UserControl; - 类的全限定名必须和XAML中
x:Class声明的值完全一致。
正确的后台代码(QuotationWizardPageView.xaml.cs)如下:
using Catel.Windows.Controls; namespace ZMAdmin.Views { public partial class QuotationWizardPageView : UserControl { public QuotationWizardPageView() { InitializeComponent(); } } } - 继承Catel提供的
修复以上问题后重新编译项目即可正常加载向导页面。如果仍有定位错误,可以查看VS输出窗口中Catel打印的定位日志,日志会明确列出框架尝试查找视图/视图模型的所有路径,方便快速排查命名不匹配问题。
内容的提问来源于stack exchange,提问作者Jeroen E
相关产品推荐
相关产品推荐

