开发者

Problem loading UserControl with DLL reference in Design View

I have a C# project with a UserControl in it.

This user control depends on a particular C++ Mixed mode dll which in turns, acts as a facade to an unmanaged C++ DLL

                     C#           C++开发者_如何学运维 Mixed            C++ Umnanaged
  [ main app ] ---> [ myUC ] ---> [ OCShell.dll ] ---> [ OCC.dll ]

In the Design View, I cannot add the UserControl. It says that there is a FileNotFoundException on OCShell (or one of its dependency). However, via code, everything works fine. In main app (windows form) I can

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. Everything gets properly copied to the Bin\Debug\ directory and each of those DLL sees each other.

My guess is that the Design View Editor does not check the proper paths for those DLL and thus returns an error.

I also tried copying every dll to every possible directory in my solution but that didn't help either


Yes, that's a problem. At issue is that the code is executing in Visual Studio, not your app. 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. 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. Which, other than the Windows side-by-side cache which requires a manifest, is limited to a directory on the system PATH environment variable.

These are not pleasant options. The best thing to do is to ensure that the code in these DLLs cannot execute at design time. You do so by using the DesignMode property, bypass calls if it is True. That needs to be done in at least the constructor and the Load event. 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. 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. 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.

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. Thanks for the solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜