Xamarin is a set of C # language-based cross-platform mobile application development tools, Microsoft announced the acquisition of Xamarin this February, and then at the April Build conference, Microsoft announced that it will provide free Xamarin in various versions of Visual Studio, and announced the Xamarin SDK 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 by Mono. Developers can use C # to develop native apps for Ios,android, Mac, and Windows Phone.
Xamarin's cross-platform development approach is to use C # to complete the platform-independent app logic part of all platforms; Because of the different UI and interaction of each platform, use the C # API packaged by Xamarin to access and manipulate the native controls, respectively, for the different platform U I developed.
Such as:
In addition Xamarin also provides the Xamarin.Forms UI toolkit, Xamarin.Forms can help developers quickly build cross-platform UIs, build a native UI interface for multiple platforms in one pass, and later in this article will describe how xamarin.forms is used and how it is implemented.
2. What Xamarin can do
Xamarin is mainly composed of xamarin.ios,xamarin.android and xamarin.forms, and the main functions are composed of three parts:
2.1 Using Xamarin.ios to build iOS native apps
The following uses Xamarin studio on Mac OS X to demonstrate how to build an iOS app:
-Open Xamarin Studio
-Create a new project and select the Ios-app-single View App
-click Next, enter the app name, for example: Firstxamarinios, click Next, Project creation completed.
The following is the resulting IOS engineering structure:
If you have Xcode experience, you will find that the project structure of the Xamarin iOS project is very similar to the structure of Xcode, including the Appdelegate class, the default Viewcontroller and the main storyboard file, The basic class names are consistent.
Open the Main.storyboard file, you can drag some native controls from the Toolbox to the view controller, and the use of the same in Xcode, but some features are not strong, such as setting the view auto layout, etc. Such as:
Run the above project and you can see the effect in the simulator.
From the above, using Xamarin for iOS programming requires a certain knowledge of iOS app development, familiarity with the iOS UI Framework (Cocoa Touch), and so on, even if you use Xamarin to develop applications, but also around the native bottom of these things.
2.2 Using Xamarin.android to build Android native apps
The following uses Xamarin studio on Mac OS X to demonstrate how to build an iOS app:
-Open Xamarin Studio
-Create a new project and select the Android-app-android App
-click Next, enter the app name, for example: Firstxamarinandroid, click Next, Project creation completed.
The following is the resulting Android engineering structure:
If Eclipse has experience with Android programming, you will find that the project structure of the Xamarin Android project is very similar to the structure of Eclipse, which contains the default mainactivity and layout files, and the basic class names are consistent.
Opening the Main.axml file allows you to drag some native controls from the Toolbox onto the view controller, similar to the Eclipse experience, or you can change the interface by editing the XML.
Also from the above point of view, using Xamarin for Android programming requires some knowledge of Android app development, familiarity with the Android UI framework, and so on, the native underlying things need to be familiar.
2.3 Using Xamarin.Forms to build cross-platform applications
Xamarin.Forms is a library that creates cross-platform user interfaces that can be coded once to generate an application interface based on each mobile platform (IOS, Android, Windows Phone).
Xamarin.Forms provides a higher level of UI component abstraction, which is presented in the form of native controls at the time of final rendering, meaning that each xmarin.forms control will eventually have multiple platform native rendering logic, such as in, Xamarin.Forms entry controls , the corresponding native rendering is:
One drawback to using xamarin.forms to build cross-platform applications is that you can only use the controls in the Xamarin.Forms package, there are some limitations.
If you learn more about how to build cross-platform applications using Xamarin.Forms, see the article: Getting Started with xamarin.forms-use xamarin.forms to create a cross-platform user interface.
3. Xamarin Implementation Principle 3.1 Xamarin.android implementation principle
Before you tell the xamarin.android architecture, you need to know the background of some Android applications:
-Android app run in Dalvik virtual machine, each application corresponds to a separate virtual machine instance, its code is executed under the explanation of the virtual machine.
-Dalvik is mainly about accomplishing object lifecycle management, stack management, thread management, security and exception management, as well as garbage collection and other important functions.
-unlike Java Virtual machine running Java bytecode, Dalvik virtual machine is running its proprietary file format
Xamarin.android Architecture Diagram (ART is an Android virtual machine Dalvik):
Android callable Wrappers (ACW)
Android applications developed using C # are running with C # code executed in the mono virtual machine, while the mono virtual machine is hosted in the Dalvik virtual machine, and all C # code is called by ACW.
Because of the need to package the mono environment, the APK files for Android apps developed with C # will be much more efficient and less effective than native developers.
Managed callable Wrapper (MCW)
If you need to invoke some system functionality or a Java-implemented class library in C #, how do I call that? The answer is that MCW,MCW is a JNI bridge that can call Android's code using managed code. MCW exposes the entire android.* and associated namespaces through the jar binding, which C # can invoke.
3.2 Xamarin.ios Implementation principle
For developers, Xamarin.ios is a lot easier than xamarin.android, and the iOS app we developed with C # is compiled into IL code and then forwarded to the Apple Complier to compile the local machine code directly into iOS, which means C #写的iOS应用程序和Objective-C writes the same.
Through the Ahead-of-time (AOT) compiler program, the Xamarin.ios program is compiled directly into arm's execution file. The compiled encapsulated application is compiled directly into the native binary execution file.
3.3 Xamarin.Forms Implementation principle
When you build xamarin.forms cross-platform apps in Xamarin Studio, you build Android and iOS separate project projects, sharing business logic and some UI interfaces that are separate when you package and build apps. The implementation of each platform is the same as above.
Full analysis of Xamarin technology