如何在 Silverlight 应用程序中使用非 Silverlight 程序集?

发布于 2024-07-17 04:21:31 字数 228 浏览 6 评论 0 原文

我正在开发一个项目(纯粹的爱好,“提高我的技能”),它有一个统一的后端和多个前端(ASP.NET MVC 1.0/JQuery 和 Silverlight 2)。 当我尝试在 Silverlight 2 项目(VS2008)中添加对我的业务层程序集的引用时; 它被拒绝,因为它不是 Silverlight 程序集。

他们是在 Silverlight 应用程序中包含和引用非 Silverlight 程序集的方法吗?

I'm working an project (pure hobby, "Sharping my skills") which has one unified back-end and multiple front-ends (ASP.NET MVC 1.0/JQuery and Silverlight 2). When I try to add reference to my business layer assembly in the Silverlight 2 project (VS2008); It gets rejected, because it's not a Silverlight assembly.

Is their a way to include and reference a non-Silverlight assembly in a Silverlight app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

自演自醉 2024-07-24 04:21:31

不,那里没有。 Silverlight 运行在完全不同的 CLR 上,该 CLR 与普通(桌面)CLR 不兼容。 它在 BCL 中具有一组不同的底层 API,最重要的是具有不同的元数据版本号。 这两个因素以及其他因素会阻止为桌面 CLR 编译的程序集默认在 Silverlight CLR 上运行。

所有程序集都必须专门针对 silverlight 进行编译。

No there is not. Silverlight runs on a completely different CLR which is incompatible with the normal (desktop) CLR. It has an underlying different set of APIs in the BCL and most importantly a different metadata version number. These two factors, among others, prevent assemblies compiled for the desktop CLR from running by default on the Silverlight CLR.

All assemblies must be compiled specifically for silverlight.

故人的歌 2024-07-24 04:21:31

实际上,虽然这很困难并且可能不是一个好主意,但是可以在 Silverlight 项目中引用 CLR 程序集。 David Betz 在他的博客上有一个例子:
http://www.netfxharmonics.com/2008/12/在 Silverlight 中重用 NET-Assemblies-in-Silverlight

再次值得强调的是,您可能并不真的想这样做。 Silverlight 框架是由经验丰富的工程师开发的,他们对应该包含什么和不应该包含什么进行了很多思考。 考虑一下您认为需要的 CLR 对象,并尝试了解它们当前不可用的原因以及替代方案是什么。

最后,请记住,您添加的任何 CLR 对象都会增加下载的大小。

Actually, whilst it is difficult and probably not a good idea, it is possible to reference CLR assemblies in a Silverlight project. David Betz has an example on his blog:
http://www.netfxharmonics.com/2008/12/Reusing-NET-Assemblies-in-Silverlight

Again it's worth stressing that you probably don't really want to do this. The Silverlight framework has been developed by experienced engineers, who have put a lot of thought into what should be included and what shouldn't. Think about the CLR objects you think you need, and try to understand why they aren't currently available, and what the alternatives are.

Finally, remember that any CLR objects that you do add, will increase the size of your download.

我不吻晚风 2024-07-24 04:21:31

不,不可能引用不是针对 Silverlight 运行时构建的程序集。

我解决这个问题的方法是为我的业务程序集创建一个新项目,然后将原始程序集中的所有类添加到其中。 关键是,当您添加它们时,请将其作为现有项目,然后在“添加”按钮上单击向下箭头并“添加为链接”。 这样,尽管您可能需要添加一些类(例如 ApplicationException)来弥补 Silverlight 运行时中缺少的内容,但您仍然只有一个代码库。

No it't not possible to reference assemblies that are not built against the Silverlight runtime.

The way I have gotten around it is to create a new project for my business assemblies and then add all the classes from the original assembly to it. The key is that when you add them do it as an existing item and on the Add button click the down arrow and Add as Link. That way you still only have a single code base although you may have to add a few classes such as ApplicationException to make up for things missing from the Silverlight runtime.

关于从前 2024-07-24 04:21:31

恐怕简短的回答是否定的。 Silverlight 运行时被设计为 .NET 框架的子集,但两者并不直接兼容。 (我相信,运行时的实现方式完全不同,因为 Silverlight 被设计为跨平台的。)

不过,好消息是您有很多解决方法。 这篇博文这篇 CodeProject 文章深入讨论了该问题并提供了各种建议干净的解决方案。 希望有帮助...

The short answer is no, I'm afraid. The Silverlight runtime was designed to be a subset of the .NET framework, but the two are not directly compatible. (The runtimes are implemented quite differently, I believe, as Silverlight was designed to be cross-platform.)

The good news however is that you have a whole host of workarounds. This blog post and this CodeProject article discuss the issue in depth and offer a variety of clean solutions. Hope that helps...

甜中书 2024-07-24 04:21:31

不可以。源 csproj 必须知道它是一个 Silverlight 项目。 这可能意味着保留两个具有相同源“.cs”文件的项目文件。 这里有一个方便的 csproj 技巧 - (复制自 protobuf-net,我对多个框架执行此操作):

<ItemGroup>
    <Compile Include="..\YourMainProject\**\*.cs" />
</ItemGroup>

那么你只需要维护一个项目; Silverlight 项目从树中获取一切。

请注意,Silverlight BCL 受到严格限制,并非所有功能都可用。 获取可在常规 .NET 和 Silverlight 上编译的代码可能...具有挑战性。

或者,在 Silverlight 应用程序中使用代理类(即通过 WCF 等)。 虽然不那么丰富,但做起来很简单。

No. The source csproj must know it is a Silverlight project. This may mean keeping two project files with the same source ".cs" files. There is a handy csproj trick here - (copied from protobuf-net where I do this for multiple frameworks):

<ItemGroup>
    <Compile Include="..\YourMainProject\**\*.cs" />
</ItemGroup>

Then you only have to maintain one project; the Silverlight project gets everything from the tree.

Note that the Silverlight BCL is heavily restricted, and not all functionality will be available. Getting code that compiles on both regular .NET and Silverlight can be... challenging.

Alternatively, use proxy classes in the Silverlight app (i.e. via WCF etc). Not as rich, but simple to do.

情绪操控生活 2024-07-24 04:21:31

警告一下,我在这方面的经验来自于 Windows Phone 7 的开发,因此这可能与普通的 Silverlight 3 略有不同。JaredPar

指出 Silverlight CLR 与普通的 CLR 不兼容。 这并不是 100% 正确,因为编译为 Windows 库的程序集仍然可以在 silverlight 下工作假设它们使用受支持的 API。您可以手动编辑 silverlight 项目并添加对普通 .NET 程序集的引用。 请注意,您只能添加对已编译程序集的引用,而不能添加对项目的引用。

silverlight 应用程序将编译并运行,但一旦它尝试使用 Silverlight 中不存在的类,就会出现运行时错误。

为了演示 API 的差异,请查看以下屏幕截图。 正如您所看到的,这两个程序集有一些通用的 API,但 Silverlight 缺少一些。 一旦您的程序集尝试访问这些 API,应用程序就会启动!

完整的 .NET 4.0 mscorlib(System.serialization 命名空间):

完整.NET 4.0 mscorlib http://img188.imageshack.us/img188/2131/fullmscorlib.png

Silverlight 3 mscorlib(System.serialization 命名空间):

Silverlight 3 mscorlib http://img526.imageshack.us/img526/4254/sl3mscorlib.png

链接的缺点完整的 .NET 程序集的缺点是,直到运行时您才知道哪些 API 不受支持。 考虑到某些受支持的系统 API 可能会使用不受支持的系统 API,因此没有简单的方法可以提前解决此问题。

您可以采取一些措施来简化并行开发。 Microsoft 推荐的方法是为.NET 和Silverlight 建立单独的项目,共享相同的源代码。 您可以通过将文件作为链接添加到项目来手动完成此操作。 这有点像维护噩梦,但至少大多数错误会在编译时被捕获。

因此,现在当您编译引用 Silverlight 中缺少的 API 的内容时,您会收到错误:

public class SerializableExample: IEquatable<string>, System.Runtime.Serialization.ISerializable
{
}

错误CS0234:命名空间“System.Runtime.Serialization”中不存在类型或命名空间名称“ISerialized”(您是否缺少程序集引用?)

条件编译的帮助下(a-la good ol'C/C++ days)您可以禁用不支持的内容:

public class SerializableExample: IEquatable<string>
#if !SILVERLIGHT
  , System.Runtime.Serialization.ISerializable
#endif
{
}

微软还提供了一个项目链接器工具,允许自动维护具有链接文件的项目。 不幸的是,当前版本不能在 VS2010 上运行,您可能可以编译源代码并实现它,但我还没有尝试过。

http://msdn.microsoft.com/en-us/library/dd458870。 aspx

直接下载链接:

http://download.microsoft.com/download/6/3/8/6382E28D-2EBD-4A4E-BB76-6F425E1C9DB9/MicrosoftPracticesProjectLinkerFeb2009.msi

这个Microsoft 页面以极其详细的方式描述了多目标定位。

A word of warning, my experience on this comes from developing for Windows Phone 7, so this might be subtly different from normal Silverlight 3.

JaredPar has pointed out the Silverlight CLR is incompatible with normal CLR. This is not 100% correct as assemblies compiled as Windows libraries will still work under silverlight assuming that they use supported APIs. You can manually edit the silverlight project and add a reference to the normal .NET assembly. Note that you can only add a reference to a compiled assembly and not the project.

The silverlight app will compile and run, but as soon as it tries to use a class that is not present in Silverlight, you get a run-time error.

To demonstrate the difference in APIs, have a look at following screenshots. As you can see the two assemblies have some common APIs, but the Silverlight one has a few missing. As soon as your assembly tries to hit those API the app goes BOOM!

Full .NET 4.0 mscorlib (System.serialization namespace):

Full .NET 4.0 mscorlib http://img188.imageshack.us/img188/2131/fullmscorlib.png

Silverlight 3 mscorlib (System.serialization namespace):

Silverlight 3 mscorlib http://img526.imageshack.us/img526/4254/sl3mscorlib.png

The disadvantage of linking a full .NET assembly is that you wouldn't know until runtime which APIs are not supported. Considering that potentially some supported system API can use unsupported system API, there is no easy way to work this out ahead of time.

There are things you can do to make parallel development easier. The approach recommended by Microsoft is to have separate project for .NET and Silverlight that share the same source code. You can do it manually by adding files as links to the project. It's a bit of a maintenance nightmare, but at least most errors will be caught at compile time.

So now when you compile something that references API missing in Silverlight you get an error:

public class SerializableExample: IEquatable<string>, System.Runtime.Serialization.ISerializable
{
}

error CS0234: The type or namespace name 'ISerializable' does not exist in the namespace 'System.Runtime.Serialization' (are you missing an assembly reference?)

With help of conditional compilation (a-la good ol' C/C++ days) you can disable stuff that is not supported:

public class SerializableExample: IEquatable<string>
#if !SILVERLIGHT
  , System.Runtime.Serialization.ISerializable
#endif
{
}

Microsoft also provide a Project linker tool that allows for automatic maintenance of projects that have linked files. Unfortunately the current release does not run on VS2010, you can probably compile the source and make it happen, but I haven't tried.

http://msdn.microsoft.com/en-us/library/dd458870.aspx

Direct download link:

http://download.microsoft.com/download/6/3/8/6382E28D-2EBD-4A4E-BB76-6F425E1C9DB9/MicrosoftPracticesProjectLinkerFeb2009.msi

This Microsoft page describes mult-targeting in excruciating detail.

鹿港巷口少年归 2024-07-24 04:21:31

Silverlight 运行时是主 .Net CLR 的子集。 虽然这看起来很痛苦,但有一个合理的原因 - Silverlight 运行时需要足够轻才能成为浏览器插件。

如果您将其他类放在 Web 服务后面,那么它们可以在完整的 .Net 运行时下运行,而您的 Silverlight 应用程序则在浏览器插件中精简的 CLR 下运行。

The Silverlight runtime is a subset of the main .Net CLR. Although this can seem to be a pain there is a sensible reason for it - the Silverlight runtime needs to be light enough to be a browser plugin.

If you place your other classes behind webservices then they can run under the full .Net runtime while your Silverlight application runs under the cut down CLR in the Browser plugin.

℉絮湮 2024-07-24 04:21:31

你试过这个吗? 它可以通过右键单击.NET库项目直接构建Silverlight Assembly。

http://buildassilverlight.codeplex.com/

Have you tried this? It can build Silverlight Assembly directly by right clicking the .NET library project.

http://buildassilverlight.codeplex.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文