使用C#和.NET的原因

时间:2021-12-26 17:25:26

  早在2000年6月,微软公布.NET之后不久,Ximian公司诞生了一个开源项目叫做Mono,运行在Linux环境下面的C#编译器和.NET Framework。十年后,在2011年,Ximian的创立者成立了Xamarin,仍然致力于开源版本的Mono,此时Mono已经适应了移动设备的跨平台。

The advantage of targeting multiple platforms with a single programming language comes from the ability to share code among the applications. 

原文

  针对多个不同的平台使用一种编程语言的优点在于可以共享应用程序之间的代码。

Before code can be shared,an application must be structured for that purpose.
Particularly since the widespread use of graphical user interfaces,programmers have understood the importance of separating application code into functional layers.
Perhaps the most useful division is between user-interface code and the underlying data models and algorithms.
The popular MVC (Model-View-Controller) application architecture formalizes this code separation into a Model (the underlying data), the View (the visual representation of the data), and the Controller (which handles input from the user). 

原文

  在开发程序之前,我们就要考虑好代码的共享问题。自从图形界面的应用大行其道,程序员开始认识到将逻辑代码从界面中分离出来,放到单独的层中有多么重要。也许最有效的分层方法是:数据层、业务逻辑层、用户界面。当下流行的MVC(模型-视图-控制器)应用程序体系结构将代码分离到模型(数据基础)、视图(数据的可视化表示)以及控制器(处理用户的输入)中。

MVC originated in the 1980s. More recently, the MVVM (Model-View-ViewModel) architecture has effectively modernized MVC based on modern GUIs. MVVM separates code into the Model (the underlying data), the View (the user interface, including visuals and input), and the ViewModel (which manages data passing between the Model and the View). 

原文

  MVC模式起源于20世纪80年代,最近,基于现代图形用户界面的MVVM模式比MVC更加灵活和有效。MVVM模式将代码分离到模型(基础数据)、视图(用户接口,包括可视化和输入)和视图模型(负责管理模型和视图直接的数据传递)。

When a programmer develops an application that targets multiple mobile platforms, the MVVM architecture helps guide the developer into separating code into the platform-specific View(the code that requires interacting with the platform APIs) and the platform-independent Model and ViewModel. 

原文

  当程序员开发一个款跨越多个移动平台的应用,MVVM架构模式可以引导开发者将代码划分为平台相关的视图(需要和平台API交互的代码)和平台无关的模型、视图模型。

Often this platform-independent code needs to access files or the network or use collections or threading. Normally these jobs would be considered part of an operating system API, but they are also jobs that can make use of the .NET Framework class library, and if .NET is available on each platform, then this code is effectively platform independent. 

原文

   通常平台无关的代码包括像文件访问、网络访问、使用集合或者线程的这类操作。很多时候,这些工作会被集成到操作系统API的当中,或者也可以通过.NET类库进行调用。如果.NET可以在每一种平台中进行调用,那么这些代码就不会对平台产生依赖。

The part of the application that is platform independent can then be isolated and—in the context of Visual Studio or Xamarin Studio—put into a separate project. This can be either a Shared Asset Project (SAP)—which simply consists of code and other asset files accessible from other projects—or a Portable Class Library (PCL), which encloses all the common code in a dynamic-link library (DLL) that can then be referenced from other projects. 

原文

  在程序中,平台无关的那一部分通常被放到一个单独的项目中。这个项目可以是一个SAP(Shared Asset Project,包含一些可以被其他项目所访问的代码和资源文件),也可以是一个PCL(Portable Class Library,可移植类库,将公共代码封装到一个DLL中,给其他项目调用)。

Whichever method you use, this common code has access to the .NET Framework class library, so it can perform file I/O, handle globalization, access web services, decompose XML, and so forth. 

原文

  不管使用的是哪一种方法,公共代码都能够访问.NET类库,所以它能够处理文件的输入/输出、访问Web服务、解析XML等等。

This means that you can create a single Visual Studio solution that contains four C# projects to target the three major mobile platforms (all with access to a common PCL or SAP), or you can use Xamarin Studio to target iPhone and Android devices. 

原文

  我们可以创建一个VS解决方案,包含四个项目,一个公共类库和针对3个主流平台的C#项目。或者可以使用Xamarin Studio创建只针对某一个平台的项目。

The following diagram illustrates the interrelationships between the Visual Studio or Xamarin Studio projects, the Xamarin libraries, and the platform APIs. The third column refers to any .NET-based Windows Platform regardless of the device: 

原文

  下图展示了Visual Studio或者Xamarin Studio项目、Xamarin 类库和平台API之间的关系。第三列指的是基于.NET的Windows平台,而不管使用的是何种设备。

使用C#和.NET的原因

The boxes in the second row are the actual platform-specific applications. These apps make calls into the common project and also (with the iPhone and Android) the Xamarin libraries that implement the native platform APIs. 

But the diagram is not quite complete: it doesn't show the SAP or PCL making calls to the .NET Framework class library. Exactly what version of .NET this is depends on the common code: A PCL has access to its own version of .NET, while an SAP uses the version of .NET incorporated into each particular platform. 

In this diagram, the Xamarin.iOS and Xamarin.Android libraries seem to be substantial, and while they are certainly important, they’re mostly just language bindings and do not significantly add any overhead to API calls. 

When the iOS app is built, the Xamarin C# compiler generates C# Intermediate Language (IL) as usual, but it then makes use of the Apple compiler on the Mac to generate native iOS machine code just like the Objective-C compiler. The calls from the app to the iOS APIs are the same as though the application were written in Objective-C. 

For the Android app, the Xamarin C# compiler generates IL, which runs on a version of Mono on the device alongside the Java engine, but the API calls from the app are pretty much the same as though the app were written in Java. 

For mobile applications that have very platform-specific needs, but also a potentially shareable chunk of platform-independent code, Xamarin.iOS and Xamarin.Android provide excellent solutions. You have access to the entire platform API, with all the power (and responsibility) that implies. 

But for applications that might not need quite so much platform specificity, there is an alternative that will simplify your life even more. 

原文

  图中的第二行指的是特定平台下的实际应用程序。应用程序去调用公共的类库项目(PLC、SAP)和实现了本地平台API的Xamarin类库。

  但上图描述的内容还不够全面,它并没有显示出PLC或SAP怎样调用.NET类库。公共代码究竟依赖于哪一个版本的.NET:一个PLC类库可以访问它自身的.NET版本,然而SAP所使用的.NET版本取决于特定的平台。

  图中的Xamarin.iOS和Xamarin.Android看上去非常重要,但是它们大多只是语言绑定,并没有显著真加对API调用的开销。

  当我们生成一个ISO应用程序的时候,Xamarin C#编译器通常会先生成C#中间语言,然后它会利用MAC上的苹果编译器去生成原生的ISO机器代码,就如同Object-C编译器一样。即使应用程序是使用的Object-C开发,应用程序调用ISO的API方式都是一样的。

  对于Android应用程序,Xamarin C#编译器生成中间语言,然后运行在一个版本的Mono设备上面,这个设备同时也安装了Java引擎。这个应用程序的API调用基本上和Java写的程序几乎一样。

  如果移动应用程序有非常依赖于具体平台的需求,还共享一个平台相关的代码块,Xamarin.iOS和Xamarin.Android有一个优秀的解决方案,你可以访问平台所有的API。

  但是对于不需要这么多平台特异性的应用程序,有更加简化的替代方法。