简体   繁体   English

从Silverlight项目添加对.net库(dll)的引用

[英]Adding a reference to a .net library (dll) from a Silverlight project

Is there a way to do this? 有没有办法做到这一点?

I have a library with 'helper' code wich I want to use from a Silverlight project. 我有一个要在Silverlight项目中使用的带有“帮助程序”代码的库。 I don't know why, but Visual Studio won't let me add the reference even though there's nothing prohibited in my library (like DataSets) 我不知道为什么,但是即使我的库中没有禁止任何内容(例如数据集),Visual Studio也不会让我添加引用

I don't want to write all that code again, so I was thinking maybe creating a Silverlight class library and adding my classes as links to that project. 我不想再次编写所有这些代码,因此我在考虑也许创建一个Silverlight类库并将类添加为该项目的链接。

I know that works for other stuff, haven't tried it in Silverlight yet, is there a problem with that approach? 我知道该方法适用于其他东西,还没有在Silverlight中尝试过,这种方法是否存在问题? is there a better way to achieve what I want? 有没有更好的方法来实现我想要的?

There shouldn't be a problem with that approach. 这种方法应该没有问题。 As a matter of fact, Prism makes heavy use of linked files (in combination with pre-processor directives) to support both WPF and Silverlight by the library. 实际上, Prism大量使用链接文件(结合预处理程序指令)来支持库的WPF和Silverlight。

Additionally, you can use the Project Linker tool to help you with this. 此外,您可以使用项目链接器工具来帮助您解决此问题。

I hope this helps. 我希望这有帮助。

No you can't add a .net assembly, you need to recompile the library for silverlight. 不,您不能添加.net程序集,您需要重新编译该库以获取Silverlight。

You can create a new empty Silverlight class library, in the same directory as the .net classlibrary. 您可以在.net classlibrary所在的目录中创建一个新的空Silverlight类库。 Then choose "Show All Files" in the project menu. 然后在项目菜单中选择“显示所有文件”。 Then you'll see all .cs files in that folder half transparent in the solution explorer. 然后,您将在解决方案资源管理器中看到该文件夹​​中的所有.cs文件半透明。 Select them and choose "Include in Project" in the context-menu. 选择它们,然后在上下文菜单中选择“包含在项目中”。

It might even be possible to just use one project with several builds(like Debug and Release some of which target SL), but I'm not sure if that's possible. 甚至可能只使用一个具有多个版本的项目(例如Debug and Release某些目标SL),但是我不确定是否可行。 This would have the advantage that you don't need to keep the project files in sync manually. 这样的好处是您不需要手动保持项目文件同步。

There is no problem with that approach. 这种方法没有问题。 MEF , for example, has done that. 例如, MEF已经做到了。 You'll find two sets of projects in there - a desktop and a silverlight project, each which refer to the same .cs files. 您将在其中找到两组项目-一个桌面项目和一个Silverlight项目,每个项目都引用相同的.cs文件。

One nice thing to help, as well - if there is any non-silverlight functionality, or SL specific functionality you require in your classes, you can use partial classes to handle those cases. 还有一件好事要帮忙-如果您的类中需要任何非Silverlight功能或特定于SL的功能,则可以使用部分类来处理这些情况。 Just put include the second .cs file in the appropriate projects, and use partial classes, and you can separate out normal/sivlerlight functionality. 只需将第二个.cs文件放入适当的项目中,并使用局部类,就可以分离出正常/ sivlerlight功能。

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

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