简体   繁体   English

在Design View中使用DLL引用加载UserControl时出现问题

[英]Problem loading UserControl with DLL reference in Design View

I have a C# project with a UserControl in it. 我有一个带有UserControl的C#项目。

This user control depends on a particular C++ Mixed mode dll which in turns, acts as a facade to an unmanaged C++ DLL 此用户控件依赖于特定的C ++混合模式dll,而后者又充当非托管C ++ DLL的外观

                     C#           C++ Mixed            C++ Umnanaged
  [ main app ] ---> [ myUC ] ---> [ OCShell.dll ] ---> [ OCC.dll ]

In the Design View, I cannot add the UserControl. 在设计视图中,我无法添加UserControl。 It says that there is a FileNotFoundException on OCShell (or one of its dependency). 它说OCShell上有一个FileNotFoundException(或者它的一个依赖项)。 However, via code, everything works fine. 但是,通过代码,一切正常。 In main app (windows form) I can 在主应用程序(Windows窗体)我可以

myUC uc = new myUC();
this.Controls.Add(uc);

and this works fine. 这很好用。 The right code gets executed properly. 正确的代码得到正确执行。

I checked with Dependency Walker and everything is ok. 我检查了Dependency Walker ,一切都很好。 Everything gets properly copied to the Bin\\Debug\\ directory and each of those DLL sees each other. 一切都被正确复制到Bin \\ Debug \\目录,每个DLL都看到对方。

My guess is that the Design View Editor does not check the proper paths for those DLL and thus returns an error. 我的猜测是,设计视图编辑器不会检查这些DLL的正确路径,从而返回错误。

I also tried copying every dll to every possible directory in my solution but that didn't help either 我也尝试将每个dll复制到我的解决方案中的每个可能的目录,但这也没有帮助

Yes, that's a problem. 是的,这是一个问题。 At issue is that the code is executing in Visual Studio, not your app. 问题是代码是在Visual Studio中执行的,而不是您的应用程序。 The probing path that's used to find dependent assemblies will only include private directories of VS (Common7\\IDE\\PrivateAssemblies and PublicAssemblies), not the build directory of your project. 用于查找依赖程序集的探测路径将仅包含VS(Common7 \\ IDE \\ PrivateAssemblies和PublicAssemblies)的私有目录,而不包括项目的构建目录。 You can make it find OCShell.dll by copying it into one of those directories, but the unmanaged DLL is going to have to be put in a directory that Window will search when looking for DLLs. 您可以通过将其复制到其中一个目录中来查找OCShell.dll,但是非托管DLL将必须放在Window将在查找DLL时搜索的目录中。 Which, other than the Windows side-by-side cache which requires a manifest, is limited to a directory on the system PATH environment variable. 除了需要清单的Windows并行缓存之外,其中只限于系统PATH环境变量上的目录。

These are not pleasant options. 这些都不是愉快的选择。 The best thing to do is to ensure that the code in these DLLs cannot execute at design time. 最好的办法是确保这些DLL中的代码不能在设计时执行。 You do so by using the DesignMode property, bypass calls if it is True. 您可以使用DesignMode属性执行此操作,如果它为True,则绕过调用。 That needs to be done in at least the constructor and the Load event. 这至少需要在构造函数和Load事件中完成。 Other events can run as well. 其他活动也可以运行。 Also greatly minimizes the odds that you'll crash Visual Studio because of a bug in the unmanaged code. 由于非托管代码中的错误,还可以极大地降低因Visual Studio崩溃的几率。 If this impacts the design-time view of the control then you may need to write a designer to make up for that. 如果这会影响控件的设计时视图,那么您可能需要编写一个设计器来弥补它。

I had the same problem. 我有同样的问题。 If you have a DLL, that uses many other DLL's, and probably written in C++, it often requires many other dependencies. 如果你有一个DLL,它使用许多其他DLL,并且可能用C ++编写,它通常需要许多其他依赖项。 In runtime, they get resolved perfectly, but not in Design mode. 在运行时,它们可以完美地解析,但不能在设计模式下解析。

Using Hans Passant's answer, you need to put this code in front of every function call, related to this DLL. 使用Hans Passant的答案,您需要将此代码放在与此DLL相关的每个函数调用之前。

if ( !DesignerProperties.GetIsInDesignMode(this) ) 

I had 2 Connect() and 2 Disconnect() calls from my DLL, and after I put this before every occasion, the Designer could now perfectly load the layout for the UserControl. 我从我的DLL中进行了2次Connect()和2次Disconnect()调用,并且在每次使用之后,Designer都可以完美地加载UserControl的布局。 Thanks for the solution. 谢谢你的解决方案。

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

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