Visual Studio 11 Beta-.NET便携式类库中的隐藏宝石

I'm realizing there's a number of subtle but important new things in the next version of VS that streamline some previously difficult tasks. For example, there's the .NET Framework, but .NET is also in Silverlight, the Windows Phone, the Xbox, etc.

我意识到在下一版的VS中有许多微妙但重要的新事物,它们简化了一些以前很困难的任务。 例如,有.NET Framework,但.NET也在Silverlight,Windows Phone,Xbox等中。

If you create a regular Class Library it has a single Target Framework. However, if you are doing a multi-platform application and you want to maximize your code reuse, you can run into trouble as you may not have all libraries available on the smaller platforms.

如果创建常规的类库,则它具有单个目标框架。 但是,如果您正在执行多平台应用程序,并且希望最大程度地重复使用代码,则可能会遇到麻烦,因为在较小的平台上可能没有所有可用的库。

Thus, Portable Class Libraries were created. You can get Portable Class Libraries via an extension on Visual Studio 2010 or they are built into Visual Studio 11 Beta.

因此,创建了可移植类库。 您可以通过Visual Studio 2010上的扩展程序获得可移植类库,或者它们已内置在Visual Studio 11 Beta中。

These Portable Class Libraries (PCLs) will generate a managed assembly that can be referenced by Windows Phone 7, Silverlight, the Microsoft .NET Framework and Xbox 360 platforms. This really helps to maximize reuse of code and reduce the number of projects in multi-targeted application solutions.

这些可移植类库(PCL)将生成一个托管程序集,Windows Phone 7,Silverlight,Microsoft .NET Framework和Xbox 360平台都可以引用该托管程序集。 这确实有助于最大程度地重复使用代码,并减少多目标应用程序解决方案中的项目数量。

I talked about multi-targeting a little in the .NET Versioning and Multi-Targeting - .NET 4.5 is an in-place upgrade to .NET 4.0 post. Visual Studio is smart enough to tell you before compilation what APIs are available. The compiler knows and Intellisense knows. The Reference Assemblies down in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\ tell you a lot.

我在.NET Versioning和Multi-Targeting中讨论了一些多目标-.NET 4.5是就地升级到.NET 4.0版本的工具。 Visual Studio非常聪明,可以在编译之前告诉您可用的API。 编译器知道并且Intellisense知道。 C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \中的“参考程序集”介绍了很多内容。

If you create a new Portable Class Library, right click on it and hit Properties. You can target specific frameworks and the build system and Intellisense will adjust. Some libraries are on some device. For example, XML Serialization isn't on the Xbox 360, but WCF is on the Windows Phone.

如果创建新的可移植类库,请右键单击它,然后单击“属性”。 您可以针对特定框架,并且构建系统和Intellisense会进行调整。 一些库在某些设备上。 例如, Xbox 360上没有XML序列化,但Windows Phone上是WCF。

Bill Kratochvil has an article in MSDN Magazine that includes the source for a project that targets Windows Phone 7, Silverlight and WPF with shared libraries. David Kean has an article this month in MSDN Mag on how to Create a Continuous Client Using Portable Class Libraries.

Bill Kratochvil在《 MSDN杂志》上有一篇文章,其中包含针对Windows Phone 7,Silverlight和带有共享库的WPF的项目的源。 David Kean本月在MSDN Mag上发表了一篇文章,内容涉及如何使用可移植类库创建连续客户端

Remember that Portable Class Libraries are constrained by design. You're targeting the lowest common denominator to maximize what you can use between projects. The MSDN article on Portable Class Libraries has a table that show what's available on each platform. You can do MVVM work across Windows, Metro style aopps, Silverlight and Windows Phone, for example.

请记住,可移植类库受设计约束。 您以最低的公分母为目标,以最大程度地利用项目之间的资源。 有关可移植类库MSDN文章中有一个表,显示了每个平台上的可用内容。 例如,您可以跨Windows,Metro风格aopps,Silverlight和Windows Phone进行MVVM工作。

Here's some good advice that David Kean sent me:

这是David Kean给我的一些好建议:

One thing [about] using portable [is that it] doesn’t mean you can’t use platform specific features, you just need to spend a little more time thinking about your dependencies. For example, instead of having a low level class that handles your persistence layer using the File APIs (which aren’t usable/available on Phone, Silverlight, etc), have it instead take work on an abstraction such as a stream, which these gets passed that from the platform specific project. Or have it call through an platform adapter (called out in my article), or inject the abstraction using your favorite IoC container (in my case Autofac, just published a portable version)

关于可移植性的一件事并不意味着您不能使用特定于平台的功能,您只需要花更多的时间来考虑您的依赖项。 例如,与其让低级类使用File API(在Phone,Silverlight等上不可用/不可用)来处理您的持久层,不如让它在诸如流之类的抽象上进行工作,这些从平台特定项目获得通过。 或通过平台适配器调用它(在我的文章中提到),或使用您喜欢的IoC容器注入抽象(在我的情况下是Autofac,刚刚发布了一个便携式版本)

MSDN Help also shows what works with Portable Libraries as well so you're supported when looking at Help, Intellisense, and at Build time.

MSDN帮助还显示了可移植库的功能,因此在查看“帮助”,“ Intellisense”和“构建”时会获得支持。

The BCL Blog mentioned that they are talking to the Mono guys about this as well. It'd be great to get Mono for Android and other frameworks as appropriate in here as well. I was excited to discover that this work was happening, even though it's been over a year in the making.

BCL博客提到,他们也正在与Mono家伙讨论此事。 也可以在这里适当地获得适用于Android和其他框架的Mono。 我很高兴发现这项工作正在进行中,尽管这项工作已经进行了一年多。

For folks like my friends at Rowi who have a great Windows Phone 7 application, or MetroTwit with a great WPF app, I wonder how Portable Class Libraries might change the architecture of their applications and enable cleaner builds and reuse scenarios they haven't thought of.

对于像Rowi这样的朋友,他们拥有出色的Windows Phone 7应用程序,或者MetroTwit具有出色的WPF应用程序,我想知道可移植类库如何改变其应用程序的体系结构并实现更清洁的构建和重用他们从未想到的方案。 。

翻译自: https://www.hanselman.com/blog/hidden-gems-in-visual-studio-11-beta-net-portable-class-libraries

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值