Full analysis of Xamarin technology

Xamarin is a set of cross-platform mobile application development tools based on C# language. In February this year, Microsoft announced the acquisition of Xamarin, and then at the Build conference in April, Microsoft announced that Xamarin will be provided for free in all versions of Visual Studio, and announced that Xamarin SDK is open source.

This article focuses on what Xamarin is, what it can do and how it is cross-platform. 

1. What is Xamarin

Xamarin is a cross-platform mobile development tool developed from Mono. Developers can use C# to develop native applications for iOS, Android, Mac and Windows Phone.

 

Xamarin's cross-platform development idea is: use C# to complete the platform-independent app logic part common to all platforms; due to the different UI and interaction of each platform, use the C# API encapsulated by Xamarin to access and manipulate native controls , respectively for UI development on different platforms.

 

As shown below:

 

In addition, Xamarin also provides the Xamarin.Forms UI toolkit. Xamarin.Forms can help developers quickly build cross-platform UI, and generate native UI interfaces for multiple platforms through one coding. Later, this article will describe the use of Xamarin.Forms methods and implementation principles.

 

2. What can Xamarin do

Xamarin is mainly composed of Xamarin.iOS, Xamarin.Android and Xamarin.Forms, and the main functions also have three parts:

 

2.1 Using Xamarin.iOS to build iOS native apps

The following will demonstrate how to build an iOS app using Xamarin Studio on Mac OS X:

- Open Xamarin Studio

- Create a new project and select iOS - App - Single View App

 

- Click Next, enter the App name, for example: FirstXamariniOS, click Next all the way, the project is created.

The following is a screenshot of the generated iOS project structure:

 

如果有Xcode使用经验的话会发现,这个Xamarin iOS工程的项目结构与Xcode的结构很类似,都包含了AppDelegate类,默认的ViewController以及Main StoryBoard文件,基本的类名称都是一致的。

 

打开Main.storyboard 文件,可以从Toolbox上拖拽一些原生控件到View Controller上,与Xcode中使用方式一致,但是有一些功能没有Xcode 强大,比如设置View的Auto layout等等,如下图:

 

运行上面的工程,就可以在模拟器中查看效果了。

 

从上面来看来说使用Xamarin进行iOS编程需要有一定的iOS App开发知识,需要熟悉iOS UI框架(Cocoa Touch)等等,即便使用Xamarin开发应用,也绕不过原生底层的这些东西。 

 

2.2 使用Xamarin.Android来构建Android原生应用

下面会使用Mac OS X上的Xamarin Studio来演示如何构建iOS应用:

- 打开Xamarin Studio

- 新建一个项目,选择Android - App - Android App

 

- 点击下一步,输入App 名称, 例如:FirstXamarinAndroid,一路点击下一步,工程创建完成。

下面是生成的Android工程结构截图:

 

如果有Eclipse进行Android编程经验的话会发现,这个Xamarin Android工程的项目结构与Eclipse的结构很类似,都包含了默认的MainActivity以及布局文件,基本的类名称都是一致的。 

打开Main.axml文件,可以从Toolbox上拖拽一些原生控件到View Controller上,与Eclipse的体验类似,也可以通过编辑XML的方式更改界面。 

同样从上面来看来说使用Xamarin进行Android编程需要有一定的Android App开发知识,需要熟悉Android UI框架等等,原生底层的东西还是需要熟悉的。 

 

2.3 使用Xamarin.Forms来构建跨平台的应用

Xamarin.Forms 是一个创建跨平台用户界面的库,通过Xamarin.Forms 可以一次编码生成基于各个移动平台(iOS, Android, Windows Phone)的应用界面。

 

Xamarin.Forms提供了更高层次的一层UI组件抽象,这些组件在进行最终呈现的时候,会以原生控件的方式表现出来,也就是说每一个Xmarin.Forms的控件最终会有多个平台的原生呈现逻辑,如下图中,Xamarin.Forms的Entry控件,对应的原生呈现为:

 

使用Xamarin.Forms构建跨平台应用的一个缺陷就是只能使用Xamarin.Forms包中的控件,会有一些限制。

如果先了解更多关于如何使用Xamarin.Forms构建跨平台应用,请参见文章:Xamarin.Forms入门-使用 Xamarin.Forms 来创建跨平台的用户界面

 

3. Xamarin实现原理

3.1 Xamarin.Android 实现原理

在讲述Xamarin.Android架构之前,需要先了解一些Android应用程序的背景知识:

- Android应用程序试运行在Dalvik虚拟机中的,每一个应用程序对应一个单独的虚拟机实例,其代码在虚拟机的解释下得以执行。

- Dalvik主要是完成对象生命周期管理,堆栈管理,线程管理,安全和异常管理,以及垃圾回收等等重要功能。

- 不同于Java虚拟机运行java字节码,Dalvik虚拟机运行的是其专有的文件格式

 

Xamarin.Android架构图(ART 是Android 虚拟机Dalvik):

 

Android Callable Wrappers(ACW)

使用C#开发的Android应用程序在运行的时候,C#代码是在Mono虚拟机中执行的,而Mono虚拟机是寄宿在Dalvik虚拟机中运行的,所有的C#代码都通过ACW的方式被调用。

 

由于需要打包Mono环境,使用C#开发的Android应用的APK文件会比原生开发的大,执行效率也会差一些。

 

Managed Callable Wrapper(MCW)

如果需要在C#中调用一些系统的功能或者Java实现的类库,该如何调用那? 答案就是MCW,MCW就是一个JNI桥梁,可以使用托管代码调用Android的代码。MCW将整个Android.* 以及相关的命名空间通过 jar绑定的方式暴露出来,是的C#可以调用。

 

3.2 Xamarin.iOS 实现原理

对于开发者来说,Xamarin.IOS相对于Xamarin.Android就要简单很多了,我们用C#开发的iOS应用程序在被编译成IL代码之后,然后转交给Apple complier直接编译成iOS的本地机器码,也就是说C#写的iOS应用程序和Objective-C 写的是一样的。 

透过 Ahead-of-Time (AOT) 编译程序,直接将Xamarin.iOS程序编译为ARM的执行档。编译封装完成的应用程序被直接编译为原生的二进制执行文件。

 

3.3 Xamarin.Forms实现原理

在Xamarin Studio中构建Xamarin.Forms跨平台的应用的时候,会生成Android以及iOS单独的项目工程,两者共享业务逻辑以及一些UI界面,在打包生成App的时候,是分开进行的,两者互不影响。每个平台的实现原理与上面讲的是一样的。

 

3.4 支持Xamarin的工具

在微软收购 Xamarin 之后,全球最大的控件公司葡萄城马上做出反应,在知名的 ComponentOne Studio 产品中加入了 Xamarin 平台。

Xamarin 平台为 Visual Studio 提供灵活的原生移动开发 ,给原生移动应用提供出色的图表、仪表盘和表格控件。

ComponentOne Studio Enterprise 是一款专注于企业应用的.NET全功能控件套包,支持 WinForms、WPF、UWP、Xamarin、ASP.NET MVC 等多个平台,帮助您在缩减成本的同时,提前交付丰富的桌面、Web和移动企业应用。控件支持的范围广泛,包含了表格和数据管理、图表和数据可视化、流行的UI界面等,为您的企业应用开发提供高性能的控件工具。

了解更多信息请访问 ComponentOne Studio官网

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679695&siteId=291194637