简体   繁体   English

在silverlight项目中引用asp.net c#classes / methods

[英]referencing asp.net c# classes/methods in silverlight project

Does anyone know how to do this? 有谁知道如何做到这一点? I built a backend c# class in asp.net but want to access these same classes without recreating them in silverlight. 我在asp.net中构建了一个后端c#类,但是想要访问这些相同的类而不用在silverlight中重新创建它们。 Is this a possibility? 这有可能吗?

You can reuse the cs files by adding them to your project AS LINK. 您可以通过将cs文件添加到项目AS LINK来重用它们。 Right click in your project and select Add Existing...Browse to your file and in the Open Button, use the pulldown arrow on the right to select Add As Link. 右键单击项目并选择Add Existing ...浏览到您的文件,然后在打开按钮中,使用右侧的下拉箭头选择Add As Link。 You will see the file added to your project with an icon that with the little Windows Shortcut icon overlayed on it. 您将看到添加到项目中的文件,其中包含一个图标,其上覆盖有小的Windows快捷方式图标。

Just remember - the ASP.Net runs on the .Net runtime. 请记住 - ASP.Net在.Net运行时运行。 Silverlight runs on the CoreCLR (Silverlight runtime.) Not everything that compiles in oone will compile in the other... Silverlight在CoreCLR(Silverlight运行时)上运行。并非在oone中编译的所有内容都将在另一个中编译...

To separate things a little bit, #if directives can help, you can also use Partial Classes and partial methods (to add content that only runs on the server or on the client.) 要稍微分开一些东西,#if指令可以提供帮助,您也可以使用部分类和部分方法(添加仅在服务器或客户端上运行的内容。)

RIA Services is definitely the way to go for sharing code between ASP.Net and Silverlight. RIA Services绝对是在ASP.Net和Silverlight之间共享代码的方式。

As well as the previously mentioned generation of domain service models, it also lets you share individual files between the web-app and Silverlight by simply inserting "shared" in to the filenames. 除了前面提到的域服务模型之外,它还允许您通过简单地将“共享”插入文件名来在Web应用程序和Silverlight之间共享单个文件。 eg "MyClass.shared.cs". 例如“MyClass.shared.cs”。

RIA services does not take long to get to terms with (and there are good tutorials about). RIA服务花了很长时间才能达成协议(并且有很好的教程)。 Try this one . 试试这个吧

Well, ASP.NET itself isn't going to work (ditto many of the full libraries), but I'm assuming you just mean you local domain model etc. 好吧,ASP.NET 本身不会起作用(同样很多完整的库),但我假设你只是指你本地域模型等。

IIRC you can try to simply reference it, but it may well generate a warning message. IIRC你可以尝试简单地引用它,但它可能会产生一个警告信息。 Of course you need to be exceptionally careful not to use anything that the other platform doesn't support... 当然,你需要格外小心,不要使用其他平台不支持的任何东西......

IMO, the better option here is to create a second csproj that includes the same .cs files (or cheat with a wildcard/deep include). IMO,这里更好的选择是创建第二个包含相同.cs文件的csproj(或使用通配符/深包含作弊)。 And build both. 并建立两者。 Same C#, different dll/platform. 相同的C#,不同的dll /平台。

Is isn't uncommon to find that you need a very small usage of #if directives, too. 发现你需要非常少量使用#if指令并不罕见。

WCF RIA Services may help you solve your problem. WCF RIA服务可以帮助您解决问题。 Silverlight does not use the same runtime as ASP.Net does and you cannot directly share assemblies containing model classes on the client and the server side. Silverlight不使用与ASP.Net相同的运行时,并且您无法直接在客户端和服务器端共享包含模型类的程序集。 To solve that WCF RIA Services will transparently generate classes on the client side based on model classes on the server side. 要解决这个问题,WCF RIA Services将根据服务器端的模型类在客户端透明地生成类。 Obviously WCF RIA Services will also allow you to create, read, update and delete objects of these classes using a web service. 显然,WCF RIA Services还允许您使用Web服务创建,读取,更新和删除这些类的对象。

MSDN has more specific information about WCF RIA Services Client Code Generation . MSDN具有有关WCF RIA服务客户端代码生成的更多特定信息。

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

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