How can I use non-Silverlight assemblies in a Silv

2019-01-18 04:27发布

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?

9条回答
闹够了就滚
2楼-- · 2019-01-18 05:15

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.

查看更多
疯言疯语
3楼-- · 2019-01-18 05:17

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...

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-18 05:17

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.

查看更多
登录 后发表回答