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

时间:2022-05-09 05:02:07

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
  2. 一个C ++ DLL

  3. a C++/CLI DLL that uses #1
  4. 使用#1的C ++ / CLI DLL

  5. a C# WPF application that uses #2
  6. 使用#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. Class1 is created, and in its constructor it calls into the C++ DLL, no problem.

但是当我在应用程序主窗口的代码隐藏中使用C ++ / CLI DLL中的类时,一切正常。创建了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设计者寻找动态库的地方有关。

2 个解决方案

#1


6  

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
  • 使用计算机 - >属性将文件夹添加到系统环境变量

  • 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。这可能有点痛苦。

#2


0  

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. And, yes: this is very annoying...

这不是一个真正的解决方案,但有时它有所帮助:使用“AnyCPU”(不是“x64”,因为设计器是32位进程)重建,并在“发布”模式下,关闭并重新打开Visual Studio。而且,是的:这非常烦人......

#1


6  

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
  • 使用计算机 - >属性将文件夹添加到系统环境变量

  • 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。这可能有点痛苦。

#2


0  

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. And, yes: this is very annoying...

这不是一个真正的解决方案,但有时它有所帮助:使用“AnyCPU”(不是“x64”,因为设计器是32位进程)重建,并在“发布”模式下,关闭并重新打开Visual Studio。而且,是的:这非常烦人......