【问题标题】:XAML designer "Could not load file or assembly Autofac 4.1" in App.xaml of UWP application using MVVMLight使用 MVVMLight 的 UWP 应用程序的 App.xaml 中的 XAML 设计器“无法加载文件或程序集 Autofac 4.1”
【发布时间】:2016-09-13 13:53:24
【问题描述】:

从一个空白的通用 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 guide to troubleshooting design time issues,尝试使用 Visual Studio 的第二个实例来调试问题,并以我能想到的各种方式搜索答案。

可以在here找到带有repo的repo。

【问题讨论】:

  • 鉴于这是一个设计时问题,您是否按照the MSDN guide to troubleshooting design time issues 中的步骤操作或搜索过有关设计时加载失败的其他答案?
  • 我不得不尝试使用另一个 Visual Studio 实例附加调试器。我没有成功。
  • 您可能应该用您已经尝试过的东西的信息来更新您的问题,这样人们就知道从哪里开始寻找,而不是重做您已经完成的工作。
  • 我知道它已经更新了。
  • 无法重现您的问题,它在我这边运行良好。您是否将 .Net Core 更新到最新的 5.2.2?

标签: c# uwp mvvm-light autofac


【解决方案1】:

随着您的project 上传,我复制了您的问题。解决这个错误的方法很简单,只需删除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.11,您将收到关于Autofac3.5.04.1.1 的更新提示。

【讨论】:

  • 软件包将此作为依赖项:Autofac (>= 3.5.0 &&
  • @Bart, Autofac 最后更新于Thursday, September 22, 2016, Autofac.Extras.CommonServiceLocator 最后更新于Tuesday, June 28, 2016。更新支持AuofacAutofac.Extras.CommonServiceLocator 不可用是可能的。依赖项显示至少版本请求。换句话说,Auofac 5.0.0 现在不存在了,对吧?这可能是未来支持的版本。
  • 我尝试使用绑定重定向,但设计师似乎也不尊重这一点。不确定这是VS中的错误还是其他问题。有趣的案例……但现在得去上班了。
  • 我同意您的建议确实使错误消失并接受它作为答案,但 Autofac 4 系列是基于 release notes 添加对 .NET Standard 1.1 的支持的地方。我很想知道为什么这个设置不起作用,或者解决方法。
  • @elanderson ,看来这个包可以单独使用,而不是CommonServiceLocator。您也可以在此处提交您的issue 以获得更多回复。
猜你喜欢
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
相关资源
最近更新 更多