简体   繁体   English

为什么 WPF 设计器无法加载调用非托管 DLL 的库?

[英]Why does the WPF designer fail to load libraries that call into unmanaged DLLs?

I am using Visual Studio 2008, .NET 3.5 SP1, and have a test application with the following modules:我正在使用 Visual Studio 2008、.NET 3.5 SP1,并且有一个包含以下模块的测试应用程序:

  1. a C++ DLL一个 C++ DLL
  2. a C++/CLI DLL that uses #1使用 #1 的 C++/CLI DLL
  3. a C# WPF application that uses #2使用 #2 的 C# WPF 应用程序

When I try to use classes from #2 as resources in the WPF XAML, the designer won't let me:当我尝试使用 #2 中的类作为 WPF XAML 中的资源时,设计者不允许我:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:lib1="clr-namespace:ClassLibrary1;assembly=ClassLibrary1" <- ERROR 

The error is: "Assembly 'ClassLibrary1' was not found. Verify that you are not missing an assembly reference. Also, verify that your project and all referenced assemblies have been built."错误是:“未找到程序集‘ClassLibrary1’。确认您没有缺少程序集引用。另外,确认您的项目和所有引用的程序集都已构建。”

But when I use a class from the C++/CLI DLL in the code-behind of the application main window, everything works fine.但是当我在应用程序 main window 的代码隐藏中使用 C++/CLI DLL 中的 class 时,一切正常。 Class1 is created, and in its constructor it calls into the C++ DLL, no problem. Class1 被创建,并在其构造函数中调用 C++ DLL,没问题。

using ClassLibrary1;

...

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        //use in code-behind
        Class1 tmp = new Class1();
        tmp.FirstName = "foo";
        Title = tmp.FirstName;
    }
}

If I modify the C++/CLI assembly, remove its call into the C++ DLL and rebuild everything, the designer stops complaining and loads the C++/CLI assembly without complaint.如果我修改 C++/CLI 程序集,删除它对 C++ DLL 的调用并重建所有内容,设计人员将停止抱怨并毫无怨言地加载 C++/CLI 程序集。

I suspect this problem has something to do with where the WPF designer looks for dynamic libraries.我怀疑这个问题与 WPF 设计者查找动态库的位置有关。

Because the Visual Studio designer copies your assemblies to a temporary location, but doesn't copy your unmanaged dependencies, you can run into this problem.由于 Visual Studio 设计器将您的程序集复制到一个临时位置,但不会复制您的非托管依赖项,因此您可能会遇到此问题。

The simplest solution, although not ideal, is to add a folder that contains your unmanaged dependencies to the PATH environment variable, and then start DevEnv.exe with that PATH .最简单的解决方案(虽然不理想)是将包含非托管依赖项的文件夹添加到PATH环境变量,然后使用该PATH启动DevEnv.exe

You can do this either by:您可以通过以下任一方式执行此操作:

  • Adding the folder to the System environment variables using Computer -> Properties使用 Computer -> Properties 将文件夹添加到系统环境变量
  • Using a batch file that sets the path and then starts DevEnv使用设置路径然后启动 DevEnv 的批处理文件

The problem with this solution is that as the unmanaged dependencies are rebuilt Visual Studio tends to "hang onto" them or not use the new ones and so you end up needing to exit and restart Visual Studio after using the designer to properly totally rebuild everything and this can be a bit of a pain.此解决方案的问题在于,随着重建非托管依赖项,Visual Studio 倾向于“挂在”它们上或不使用新的依赖项,因此您最终需要在使用设计器正确地完全重建所有内容后退出并重新启动 Visual Studio,并且这可能有点痛苦。

Not a real solution, but sometimes it helps: rebuild using "AnyCPU" (not "x64", because the designer is a 32bit process) and in "Release" mode, close and re-open Visual Studio.不是真正的解决方案,但有时它会有所帮助:使用“AnyCPU”(不是“x64”,因为设计器是 32 位进程)重建并在“发布”模式下,关闭并重新打开 Visual Studio。 And, yes: this is very annoying...而且,是的:这很烦人......

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

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