Prism WPF AutoWireViewModel 异常

问题描述

我决定用 WPF 和 Prism 做一个重要的演示应用程序,以便在做的时候学习一些很酷的东西。一切都很顺利,直到我在 UI 层和数据服务层之间添加一个业务逻辑层 (PartsAnalysis.Core)。出于某种原因,我得到了这个奇怪的 Prism.Mvvm.viewmodelLocator.AutoWireviewmodel 异常:

    at http://System.Windows.Markup.XamlReader.RewrapException(Exception e,IXamlLineInfo lineInfo,Uri baseUri)
   at http://System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,Boolean skipJournaledProperties,Object rootObject,XamlObjectWriterSettings settings,Uri baseUri)
   at http://System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,XamlAccessLevel accessLevel,Uri baseUri)
   at http://System.Windows.Markup.XamlReader.LoadBaml(Stream stream,ParserContext parserContext,Object parent,Boolean closeStream)
   at http://System.Windows.Application.LoadComponent(Object component,Uri resourceLocator)
   at Footer.Views.Footer.InitializeComponent() in C:\Users\akahaei\source\repos\PartsAnalysis\FootersModule\Views\Footer.xaml:line 1
   at Footer.Views.Footer..ctor() in C:\Users\akahaei\source\repos\PartsAnalysis\FootersModule\Views\Footer.xaml.cs:line 12

添加业务逻辑层之前,视图和视图模型链接良好。

这是 FooterView.xaml:

<UserControl x:Class="Footer.Views.FooterView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Footer.Views"
         xmlns:prism="http://prismlibrary.com/"
         prism:viewmodelLocator.AutoWireviewmodel="True"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Orientation="Vertical">
    <!--<TextBlock Text="{Binding Title}" />-->
    <!--Todo: add localization-->
    <Button Command="{Binding ImportDataCommand}">IMPORT</Button>
</StackPanel>

通过试验,我注意到如果我从视图模型的构造函数删除 IMeasurementAnalysermeasurementAnalyser 注入(属于业务逻辑层),我不会再遇到该异常:

public Footerviewmodel(IMeasurementAnalyser measurementAnalyser,IEventAggregator eventAggregator)
{
     _measurementAnalyser = measurementAnalyser;
     _eventAggregator = eventAggregator;
     ImportDataCommand = new DelegateCommand(ImportData).ObservesCanExecute(() => CanImportExecute);
     SendMeasurementsCommand = new DelegateCommand(SendMeasurements);
}

这是它在 FooterModule 中的注册方式:

public class FooterModule : IModule
{
    private readonly IRegionManager _regionManager;

    public FooterModule(IRegionManager regionManager)
    {
        _regionManager = regionManager;
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSingleton<IMeasurementAnalyser,MeasurementAnalyser>();
    }

    public void OnInitialized(IContainerProvider containerProvider)
    {
        _regionManager.RegisterViewWithRegion(RegionNames.FooterRegion,typeof(Views.Footer));
    }
}

这是模块目录:

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        moduleCatalog.AddModule<PlotModule>();
        moduleCatalog.AddModule<FooterModule>();
        moduleCatalog.AddModule<CoreModule>();
        moduleCatalog.AddModule<DataAccessModule>();
    }

The dependency diagram between the projects

我已经花了几个小时,但我仍然不明白出了什么问题。我还尝试覆盖惯例以及我想到的任何内容,但没有。

注意:我在所有项目中都使用 .NET 5。

解决方法

由于我的声誉,我无法发表评论,但是否有可能在 MeasurementAnalyser 的构造函数中发生异常或出现问题。 如果我尝试使用您的演示应用程序的设置在测试应用程序中复制您的问题,如果我在服务寄存器的构造函数中抛出异常作为单例,我将得到抛出的异常以及几乎相同的自动装配异常堆栈跟踪