在 Windows 和 Silverlight 类库之间共享 C# 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/465460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:29:56  来源:igfitidea点击:

Sharing C# code between Windows and Silverlight class libraries

c#windowssilverlightsharing

提问by Steve Crane

We wrote a small Windows class library that implements extension methods for some standard types (strings initially). I placed this in a library so that any of our projects would be able to make use of it by simply referencing it and adding using XXX.Extensions.

我们编写了一个小的 Windows 类库,它实现了一些标准类型(最初是字符串)的扩展方法。我把它放在一个库中,这样我们的任何项目都可以通过简单地引用它并使用 XXX.Extensions 添加来使用它。

A problem came up when we wanted to use some of these methods in Silverlight. Although all the code was compatible, a Windows library can't be referenced in Silverlight so we created a Silverlight library that had links to the same class files and put compiler directives into the classes to allow different using declarations and namespaces. This worked fine until today when I added a new class to the Windows extensions library and realised that I would have to remember to link the class into the Silverlight library too.

当我们想在 Silverlight 中使用其中一些方法时出现了一个问题。尽管所有代码都是兼容的,但不能在 Silverlight 中引用 Windows 库,因此我们创建了一个 Silverlight 库,该库具有指向相同类文件的链接,并将编译器指令放入类中以允许不同的 using 声明和命名空间。直到今天,当我向 Windows 扩展库添加一个新类并意识到我还必须记住将类链接到 Silverlight 库时,这一切正常。

This isn't ideal and I wondered if anyone might have ideas for a better way of sharing extension methods and other helper code between Windows and Silverlight projects.

这并不理想,我想知道是否有人可能对在 Windows 和 Silverlight 项目之间共享扩展方法和其他帮助程序代码的更好方法有想法。

采纳答案by Maurice

You cannot set a reference from a Silverlight assembly to a regular .NET assembly but you can do so the other way round.

您不能将 Silverlight 程序集的引用设置为常规 .NET 程序集,但您可以反过来这样做。

So create a shared Silverlight assembly and add your code to that assembly. Now you can set a reference fro both your regular .NET and you other Silverlight assembly to the shared Silverlight assembly.

因此,创建一个共享的 Silverlight 程序集并将您的代码添加到该程序集。现在,您可以将常规 .NET 和其他 Silverlight 程序集的引用设置为共享 Silverlight 程序集。

The restriction is that you can only put code in there that would work on both the .NET and Silverlight CLR but that is no different from sharing code.

限制是您只能将可以在 .NET 和 Silverlight CLR 上运行的代码放在那里,但这与共享代码没有什么不同。

回答by Joel Martinez

there is a similar problem with XNAprojects. Since you can target several different platforms, you're required to have different projects. The reason for this is because the base class libraries that the project references are platform specific, so you can't have just one project.

XNA项目也存在类似的问题。由于您可以针对多个不同的平台,因此您需要拥有不同的项目。这是因为项目引用的基类库是特定于平台的,因此您不能只有一个项目。

If you're curious, you can get a bit of insight from this blog:

如果你很好奇,你可以从这个博客中获得一些见解:

To recompile the source for another platform, you need another project. The reason for this is because the projects need to reference different assemblies for both the XNA Framework and the underlying .NET Framework (Xbox 360 and Zune use the .NET Compact Framework), and C# projects don't provide support for referencing different assemblies for different platforms.

要为另一个平台重新编译源代码,您需要另一个项目。这是因为项目需要为 XNA Framework 和底层 .NET Framework(Xbox 360 和 Zune 使用 .NET Compact Framework)引用不同的程序集,而 C# 项目不支持引用不同的程序集不同的平台。

回答by Rinat Abdullin

Silverlight runtime is different from the normal .NET runtime. So you need to do tricks at the project level to share code between multiple platforms.

Silverlight 运行时不同于普通的 .NET 运行时。所以你需要在项目层面做一些技巧来在多个平台之间共享代码。

Here's how I've done this for Autofac IoC container.

这是我如何为 Autofac IoC 容器完成此操作

With this approach you do not have to create different projects for each platform being targeted.

使用这种方法,您不必为每个目标平台创建不同的项目

PS: there is also a Project Linkertool from the Composite WPFthat allows to link Silverlight and WPF projects (creates multiple projects). But it does look messy.

PS:还有一个来自Composite WPFProject Linker工具,它允许链接 Silverlight 和 WPF 项目(创建多个项目)。但看起来确实很乱。

回答by Johan

I had some dependency issues when referencing to a Silveright class library in .Net.

引用 .Net 中的 Silveright 类库时,我遇到了一些依赖性问题。

An alternative way when using Visual Studio 2010 and WCF RIA 1.0:

使用 Visual Studio 2010 和 WCF RIA 1.0 时的另一种方法:

  • Create a normal .Net library assembly.
  • Create a Silverlight class library. In the configuration of the assembly specifiy the first .NET library as the "WCF RIA Service link"
  • Put your code in the .NET library as "ClassName.shared.cs" files.
  • WCF RIA will handle copying the file to the Silverlight assembly.
  • 创建一个普通的 .Net 库程序集。
  • 创建 Silverlight 类库。在程序集的配置中,将第一个 .NET 库指定为“WCF RIA 服务链接”
  • 将您的代码作为“ClassName.shared.cs”文件放入 .NET 库中。
  • WCF RIA 将处理将文件复制到 Silverlight 程序集。

回答by Andrew Barrett

Since this question has been answered, there is a new solution from Microsoft, Portable Class Libraries. Hereis the blog post where they announced it.

既然已经回答了这个问题,微软就有了一个新的解决方案,可移植类库是他们宣布的博客文章。

I'm just about to start playing round with them for sharing code between silverlight and some server side code I'm writing, so can't add too much past the links at the moment.

我正准备开始和他们一起玩,以便在 Silverlight 和我正在编写的一些服务器端代码之间共享代码,所以目前不能在链接之外添加太多内容。