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. 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)
开发者_开发百科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.
I know that works for other stuff, haven't tried it in Silverlight yet, is there a problem with that approach? 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.
Additionally, you can use the Project Linker tool to help you with this.
I hope this helps.
There is no problem with that approach. MEF, for example, has done that. You'll find two sets of projects in there - a desktop and a silverlight project, each which refer to the same .cs files.
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. Just put include the second .cs file in the appropriate projects, and use partial classes, and you can separate out normal/sivlerlight functionality.
No you can't add a .net assembly, you need to recompile the library for silverlight.
You can create a new empty Silverlight class library, in the same directory as the .net classlibrary. 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. 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. This would have the advantage that you don't need to keep the project files in sync manually.
精彩评论