繁体   English   中英

使用MVVMLight的UWP应用程序的App.xaml中的XAML设计器“无法加载文件或程序集Autofac 4.1”

[英]XAML designer “Could not load file or assembly Autofac 4.1” in App.xaml of UWP application using MVVMLight

从空白的通用Windows应用程序开始,我添加了Autofac 4.1,Autofac.Extras.CommonServiceLocator 4.0和MvvmLightLibs 5.3。 然后我创建了以下ViewModelLocator类。

using Autofac;
using Autofac.Extras.CommonServiceLocator;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;

namespace UwpTest
{
    public class ViewModelLocator
    {
        public static IContainer Container { get; private set; }
        public static bool IsBuilt { get; set; }

        public ViewModelLocator()
        {
            if (!IsBuilt)
            {
                var builder = new ContainerBuilder();
                builder.RegisterType<DialogService>().As<IDialogService>();
                builder.RegisterType<NavigationService>().As<INavigationService>();

                Container = builder.Build();

                IsBuilt = true;
            }

            ServiceLocator.SetLocatorProvider(() => new AutofacServiceLocator(Container));
        }
    }
}

然后在App.xaml中我添加了视图模型定位器作为资源。

<Application
    x:Class="UwpTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UwpTest"
    RequestedTheme="Light">
    <Application.Resources>
        <ResourceDictionary>
            <local:ViewModelLocator x:Key="ViewModelLocator" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

此时我收到以下错误:

无法加载文件或程序集'Autofac,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = 17863af14b0044da'或其依赖项之一。 该系统找不到指定的文件。

一切都在运行时正常,但不是在设计时。 我仔细检查了所有包都已恢复。 如何找到设计器尝试加载Autofac dll的位置? 或者我对项目的设置方式缺少什么?

我已经通过MSDN指南来解决设计时间问题 ,尝试使用Visual Studio的第二个实例调试问题,并以我能想到的各种方式搜索答案。

可以在这里找到回购问题的回购。

上传您的项目后,我再现了您的问题。 解决此错误的解决方案很简单,只需删除Autofac 4.1.1包即可。

这是因为当您安装Autofac.Extras.CommonServiceLocator NuGet包时,它将帮助您同时安装包含Autofac包的依赖项。 您无需自行安装Autofac软件包。 当您安装Autofac.Extras.CommonServiceLocator 4.0.0 ,它将帮助您安装Autofac 3.5.0 如果您安装另一个版本的Autofac软件包,可能会导致歧义。

如果您确实想要同时添加两个软件包,请将Autofac版本从4.1.1更新到3.5.0 它也会很好,但我认为没有必要。

我如何知道Auofac3.5.0版本,请参见下图。 如果您在安装Autofac.Extras.CommonServiceLocator 4.0.0之后尝试安装Autofac 4.11Autofac3.5.04.1.1获得有关Autofac的更新提示。

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM