When you first created the Hello solution in Visual Studio, you had a choice of two application templates: Blank App (Xamarin.Forms Portable) Blank App (Xamarin.Forms Shared) In Xamarin Studio, the choice is embodied in a pair of radio buttons: Use Portable Class Library Use Shared Library The first option creates a Portable Class Library (PCL), whereas the second creates a Shared Asset Project (SAP) consisting only of shared code files. The original Hello solution used the PCL template. Now let’s create a second solution named HelloSap with the SAP template.
原文
当我们在VS中创建Hello解决方案的时候,可以选择两种类型的项目模板。第一个选项创建的是可移植的类库(PCL),而第二个创建的是共享的资源项目(SAP),仅仅包含一个共享文件。之前的使用的是PCL模板,下面使用SAP模板创建一个解决方案HelloSap。
As you’ll see, everything looks pretty much the same, except that the HelloSap project itself contains only one item: the App.cs file. With both the PCL and SAP approaches, code is shared among the five applications, but in decidedly different ways: With the PCL approach, all the common code is bundled into a dynamic-link library that each application project references and binds to at run time. With the SAP approach, the common code files are effectively included with each of the five application projects at build time. By default, the SAP has only a single file named App.cs, but effectively it’s as if this HelloSap project did not exist and instead there were five different copies of this file in the five application projects. Some subtle (and not-so-subtle) problems can manifest themselves with the shared library approach: The iOS and Android projects have access to pretty much the same version of .NET, but it is not the same version of .NET that the Windows projects use. This means that any .NET classes accessed by the shared code might be somewhat different depending on the platform. As you’ll discover later in this book, this is the case for some file I/O classes in the System.IO namespace. You can compensate for these differences by using C# preprocessor directives, particularly #if and #elif. In the projects generated by the Xamarin.Forms template, the various application projects define symbols that you can use with these directives.
原文
创建结束后,可以看到HelloSap解决方案中,除了HelloSap这个项目之外,其他的几个项目都和我们使用PCL模板创建出来的类似。
不管你使用的是PCL还是SAP,都会被另外几个项目所引用,但是二者有着不同之处。如果使用PCL,所有的公共代码是生成到一个动态链接库(dll)中去,然后其他项目在运行的时候去引用这个dll。然而,SAP方法中,在编译的时候公共代码直接包含在其他的几个项目中了。默认情况下SAP项目只有一个cs文件,如果HelloSap这个项目不存在,完全可以在其他项目中包含这个cs文件来代替这个项目。
...
Most of the programs in this book use the PCL approach. This is the recommended approach for Xamarin.Forms and is preferred by many programmers who have been working with Xamarin.Forms for a while. However, the SAP approach is also supported and definitely has its advocates as well. Programs within these pages that demonstrate the SAP approach always contain the letters Sap at the end of their names, such as the HelloSap program. But why choose? You can have both in the same solution. If you’ve created a Xamarin.Forms solution with a Shared Asset Project, you can add a new PCL project to the solution by selecting the Class Library (Xamarin.Forms Portable) template. The application projects can access both the SAP and PCL, and the SAP can access the PCL as well.
原文
这本书的大部分示例程序使用的是PCL方法,它是Xamarin.Froms推荐的方法,并且是多数开发者一直所使用的。但是,这两个项目可以同时包含在一个解决方案中。