无法加载文件或程序集 'Xceed.Wpf.Toolkit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22108712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:04:01  来源:igfitidea点击:

Could not load file or assembly 'Xceed.Wpf.Toolkit

c#wpfwpftoolkitrevitrevit-api

提问by Eric Anastas

I'm developing an add-in for another application, Autodesk Revit, which is built as a separate DLL class library. I'm trying to use the Wpf Tool Kit Property gridin one of my WPF windows. The property grid displays fine in Visual studio, and intellisense works as well. But When I try to start Revit with my add-in loaded I get the following exception.

我正在为另一个应用程序 Autodesk Revit 开发加载项,它是作为单独的 DLL 类库构建的。我正在尝试在我的 WPF 窗口之一中使用Wpf 工具包属性网格。属性网格在 Visual Studio 中显示良好,并且智能感知也能正常工作。但是,当我尝试在加载了加载项的情况下启动 Revit 时,出现以下异常。

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message=Could not load file or assembly 'Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4' or one of its dependencies. The system cannot find the file specified.
  Source=PresentationFramework
  LineNumber=133
  LinePosition=27
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
  InnerException: System.IO.FileNotFoundException
       HResult=-2147024894
       Message=Could not load file or assembly 'Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4' or one of its dependencies. The system cannot find the file specified.
       Source=mscorlib
       FileName=Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4
       FusionLog==== Pre-bind state information ===
LOG: User = GLOBAL\eric.anastas
LOG: DisplayName = Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4 | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Program Files/Autodesk/Revit 2014/
LOG: Initial PrivatePath = NULL
Calling assembly : PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files\Autodesk\Revit 2014\Revit.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/SDA/bin/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/SDA/bin/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/SDA/bin/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit 2014/SDA/bin/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE.

       StackTrace:
            at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
            at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
            at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
            at System.Windows.Baml2006.Baml2006SchemaContext.ResolveAssembly(BamlAssembly bamlAssembly)
            at System.Windows.Baml2006.Baml2006SchemaContext.ResolveBamlTypeToType(BamlType bamlType)
            at System.Windows.Baml2006.Baml2006SchemaContext.ResolveBamlType(BamlType bamlType, Int16 typeId)
            at System.Windows.Baml2006.Baml2006SchemaContext.GetXamlType(Int16 typeId)
            at System.Windows.Baml2006.Baml2006Reader.Process_ElementStart()
            at System.Windows.Baml2006.Baml2006Reader.Process_OneBamlRecord()
            at System.Windows.Baml2006.Baml2006Reader.Process_BamlRecords()
            at System.Windows.Baml2006.Baml2006Reader.Read()
            at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
            at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       InnerException: 

Typically when I want to reference a 3rd party assembly from a Revit plug-in I simply make sure the 3rd party DLL is being copied to the same location as my plug-in DLL. I checked and Xceed.Wpf.Toolkit.dllis being copied to the directory containing my plug-in DLL.

通常,当我想从 Revit 插件引用 3rd 方程序集时,我只需确保将 3rd 方 DLL 复制到与我的插件 DLL 相同的位置。我检查并Xceed.Wpf.Toolkit.dll正在复制到包含我的插件 DLL 的目录。

I noticed from the log messages in the error that it's looking for the DLL in the Revit program directory. After copying Xceed.Wpf.Toolkit.dllinto this directory I no longer received the error.

我从错误中的日志消息中注意到它正在 Revit 程序目录中查找 DLL。复制Xceed.Wpf.Toolkit.dll到此目录后,我不再收到错误消息。

However, I have existing plug-in deployment tools which depend on the plug-in files being in their own isolated folder.

但是,我现有的插件部署工具依赖于插件文件位于它们自己的独立文件夹中。

So, does anyone have any idea how I can get the plug-in to find the WPF Toolkit library?

那么,有没有人知道如何获取插件以查找 WPF Toolkit 库?

回答by Eric Anastas

So, I found a new and better solution to this question of mine from 2014.

因此,我从 2014 年开始为我的这个问题找到了一个新的更好的解决方案。

Today I ran into the same problem where loading a WPF control from an assembly would throw an XamlParseException, except this time it was with a WPF control library assembly that I had created.

今天我遇到了同样的问题,从程序集加载 WPF 控件会抛出 XamlParseException,但这次是使用我创建的 WPF 控件库程序集。

I tried moving the DLL into the same folder as the EXE, and as before this solved the problem.

我尝试将 DLL 移动到与 EXE 相同的文件夹中,并且和以前一样解决了问题。

After some searching, I found this question on the telerik.com forums: http://www.telerik.com/forums/xamlparseexception-could-not-load-file-or-assembly

经过一番搜索,我在 Telerik.com 论坛上发现了这个问题:http: //www.telerik.com/forums/xamlparseexception-could-not-load-file-or-assembly

It turns out if you simply give the control a name, by adding a x:Nameattribute, this will add a reference to the control in the code-behind and for some reason resolves the issue with loading the assembly.

事实证明,如果您只是简单地为控件命名,通过添加x:Name属性,这将在代码隐藏中添加对控件的引用,并且由于某种原因解决了加载程序集的问题。

  <!--This causes a XamlParseException -->
  <mylib:MyCustomControl />

  <!-- This works -->
  <mylib:MyCustomControl x:Name="foobar"/>

回答by Matt

I am a fan of this approach. You can register an event on the AppDomain for the AssemblyResolve event, which catches when an assembly cannot be loaded.

我是这种方法的粉丝。您可以在 AppDomain 上为 AssemblyResolve 事件注册一个事件,该事件在无法加载程序集时捕获。

It looks like this:

它看起来像这样:

// using System.Reflection and System.IO

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args )
{
    if (args.Name.ToUpper().StartsWith("XCEED.WPF"))
    {
       string asmLocation = Assembly.GetExecutingAssembly().Location;

       string asmName = args.Name.Substring(0, args.Name.IndexOf(','));
       string filename = Path.Combine( asmLocation, asmName );

       if (File.Exists(filename)) return Assembly.LoadFrom(filename);
    }
}

You can make it a little more complete than this, but you get the idea...

你可以让它比这更完整一点,但你明白了......

回答by John August

I know that this is a very old question but I happened to run across this exact error not too long ago. If your visual studio application uses two projects or a project that references another project, I would check to make sure that BOTH projects have the extended toolkit installed.

我知道这是一个非常古老的问题,但不久前我碰巧遇到了这个确切的错误。如果您的 Visual Studio 应用程序使用两个项目或一个引用另一个项目的项目,我会检查以确保两个项目都安装了扩展工具包。

Right click on both your projects and click, "Manage NuGet Packages" and then browse on the left hand side of the dialogue to "Installed Packages". If you do not see extended toolkit on both projects then you can use the manager to search online and install them for you.

右键单击您的两个项目并单击“管理 NuGet 包”,然后在对话框的左侧浏览到“已安装的包”。如果您在两个项目中都没有看到扩展工具包,那么您可以使用管理器在线搜索并为您安装它们。

My issue was that I only had the extended toolkit installed on one project not both.

我的问题是我只在一个项目上安装了扩展工具包,而不是两个。

Hopefully this helps someone out in the future.

希望这可以帮助将来的某个人。

回答by Klaus Nji

Although this has probably been solved, a common reason is failure to add the Xceed.Wpf.Toolkit dll to your entry point project. You probably added it to one of your class libary projects and set its "Copy Local" attribute to true. A reference to this dll also has to be added to your main project which contains your App.xaml.cs with its "Copy Local" attribute set to true.

虽然这可能已经解决,但一个常见的原因是未能将 Xceed.Wpf.Toolkit dll 添加到您的入口点项目。您可能将它添加到您的类库项目之一并将其“复制本地”属性设置为 true。还必须将对此 dll 的引用添加到包含 App.xaml.cs 且“Copy Local”属性设置为 true 的主项目中。

I am surprised Visual Studio 2013 does not handle this automatically.

我很惊讶 Visual Studio 2013 没有自动处理这个。

回答by Daren Thomas

While I personally believe you should be doing it as stated in the accepted answer (by @Matt), I would like to mention that copying the dll to the "Program" folder in the Autodesk Revit installation would probably also do the trick. If I recall correctly, they also suggest you deploy your addins to a subfolder of this folder to ensure that it just works. I suspect, that is due to effects like the one you are having.

虽然我个人认为您应该按照接受的答案(@Matt)中的说明进行操作,但我想提一下,将 dll 复制到 Autodesk Revit 安装中的“程序”文件夹可能也能解决问题。如果我没记错的话,他们还建议您将插件部署到此文件夹的子文件夹中,以确保它正常工作。我怀疑,这是由于您所拥有的影响。

回答by jamesthollowell

In the System.Reflectionnamespace, there is the Assemblyclass. This can be used to load new assemblies into the current AppDomain.

System.Reflection命名空间中,有Assembly类。这可用于将新程序集加载到当前 AppDomain 中。

Assembly.LoadFrom("FileLocation");

Although this is still annoying, I think this might be the only way to have it load a library not in the main directory.

虽然这仍然很烦人,但我认为这可能是让它加载不在主目录中的库的唯一方法。

回答by Paul Hildebrandt

Make sure you "Unblock" the Xceed assembly. Right click on the file and select properties then "Unblock". The VS will compile the code without any errors but when you go to run, Windows will not load the assembly. Mine was even merged into a single assembly.

确保“取消阻止”Xceed 程序集。右键单击该文件并选择属性,然后选择“取消阻止”。VS 将编译代码而不会出现任何错误,但是当您运行时,Windows 不会加载程序集。我的甚至合并为一个程序集。

回答by Markus Weber

I used Fody Costurato embedd the Xceed.Wpf.Toolkit.dllin the compiled .dll. Just install it using NuGet and type Install-CleanReferencesTargetinto your Packet Manager Consoleand there you go.

我用Fody Costura以使嵌入的Xceed.Wpf.Toolkit.dll在编.dll。只要使用的NuGet安装它并输入Install-CleanReferencesTarget到你Packet Manager Console和你去。

回答by Maxence

When the control is loaded from the XAML, the calling assembly from which your Xceed.Wpf.Toolkit.dllis loaded is PresentationFramework.dll. So the CLR will not look into your addin folder in this case (what it's doing when another class is loaded from the main assembly of your addin, because it looks into the folder of the calling assembly).

从 XAML 加载控件时,从中加载的调用程序集Xceed.Wpf.Toolkit.dllPresentationFramework.dll. 因此,在这种情况下,CLR 不会查看您的插件文件夹(当从您的插件的主程序集中加载另一个类时它正在做什么,因为它会查看调用程序集的文件夹)。

So you can as you've find, put a reference to the control in the code-behind or you can use the AppDomain.CurrentDomain.AssemblyResolveto force the CLR to look in your addin folder.

因此,您可以按照您的发现,在代码隐藏中放置对控件的引用,或者您可以使用AppDomain.CurrentDomain.AssemblyResolve强制 CLR 在您的插件文件夹中查找。

Putting the dll in the Revit installation folder works, but it's a bad practice in my point of view because it can be overwritten by another addin installation with consequences that are difficult to measure.

将 dll 放在 Revit 安装文件夹中是可行的,但在我看来这是一个不好的做法,因为它可能会被另一个插件安装覆盖,后果难以衡量。

回答by tom-englert

I've seen people using this workarounds for Visual Studio Extensions. However VS provides a better solution, using the "ProvideCodeBaseAttribute":

我见过有人在 Visual Studio 扩展中使用这种变通方法。然而 VS 提供了更好的解决方案,使用“ProvideCodeBaseAttribute”:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.shell.providecodebaseattribute

https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.shell.providecodebaseattribute