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

只谈情不闲聊 提交于 2019-12-13 17:31:17

问题


Starting with a blank Universal Windows Application I added Autofac 4.1, Autofac.Extras.CommonServiceLocator 4.0 and MvvmLightLibs 5.3. I then created the following ViewModelLocator class.

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));
        }
    }
}

Then in the App.xaml I added the view model locator as a resource.

<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>

At this point I get the following error:

Could not load file or assembly 'Autofac, Version=4.1.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The system cannot find the file specified.

Everything works fine at run time, but not at design time. I have double check that all packages have been restored. How can I find the location that the designer is attempting to load the Autofac dll from? Or what am I missing about how the project is setup?

I have already been though the MSDN guide to troubleshooting design time issues, tried to debug the issue using a second instance of Visual Studio, and searched for answer in every way I can think of.

Repo with repo of the issue can be found here.


回答1:


With your project uploaded I reproduced your issue. The solution for resolving this error is easy, just remove the Autofac 4.1.1 package it will be fine.

This is because when you install the Autofac.Extras.CommonServiceLocator NuGet package, it will help you install the dependencies which contain the Autofac package at the same time. You don't need to install the Autofac package by yourself. When you installed Autofac.Extras.CommonServiceLocator 4.0.0 it will help you install Autofac 3.5.0. If you install another version Autofac package it may lead ambiguity.

If you do want add two packages at the same time, please update the Autofac version from 4.1.1 to 3.5.0. And it will also be fine, but I don't think it is necessary.

For how I know the Auofac is version 3.5.0 please see the following picture. If you tried to install the Autofac 4.11 after the Autofac.Extras.CommonServiceLocator 4.0.0 installed, you will get a update prompt about Autofac from 3.5.0 to 4.1.1.



来源:https://stackoverflow.com/questions/39471849/xaml-designer-could-not-load-file-or-assembly-autofac-4-1-in-app-xaml-of-uwp-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!