C # Mobile cross-platform development (2) How does Xamarin mobile cross-platform solution work?

Source: Internet
Author: User
Tags wrappers

Overview

Previous C # Mobile cross-platform development (1) The environment was ready to be released shortly after, coincidentally, Microsoft announced the opening. NET Framework source code and will develop a core runtime (core CLR) for Windows, Mac, and Linux, which is also open source! It media sites have been reproduced, the blog Park's C # developers are tears (peat has been waiting for a long time!) )

At the same time, the VS2015 preview is directly integrated with the Android emulator, but in fact it does not say that integrated iOS simulator, I do not know how you can directly use VS to develop Android and iOS applications. Anyway, this is good news. So the question is, how does C # develop Android and iOS apps? We're not sure what Microsoft will do, but we can look at how Xamarin is doing it.

Android System Architecture

I think the following figure for Android development students should be very familiar with, below we will learn about the Android system architecture Introduction to see how Xamarin will do?

    • Linux Kernel OS Layer
    • Libraries and Android runtime various libraries and Android runtime environment
    • Application Framework Application frame layer (written by Java)
    • Applications application layer (written by Java and run on DALVK virtual machines)

Now the students do Android development as long as they are familiar with the application framework layer of some of the interface and class library can be convenient to implement their own Android applications.

About the difference between the DALVK virtual machine and the Java operating environment
    1. Dalvik mainly includes the completion of object lifecycle management, stack management, thread management, security and exception management, as well as garbage collection and other important functions.
    2. Dalvik is responsible for process isolation and thread management, and each Android application will have a standalone Dalvik virtual machine instance at the bottom, and its code can be executed under the virtual machine interpretation.
    3. Unlike Java virtual machines running Java bytecode, the Dalvik virtual machine is running its own proprietary file format
    4. The Dex file format reduces the overall file size and improves the class lookup speed of I/O operations.
    5. is to further improve performance during operation and further optimize the Dex file.
    6. All Android apps have threads that correspond to one Linux thread, so virtual machines can rely more on the operating system's thread scheduling and management mechanisms.
    7. There is a special virtual machine process zygote, which is an incubator of virtual machine instances. It will be generated when the system starts, it will complete the initialization of the virtual machine, library loading, prefabricated class library and initialization operations. If the system needs a new instance of the virtual machine, it will quickly replicate itself and provide the system with the fastest data. For some read-only system libraries, all virtual machine instances share a memory area with zygote.

Attention to 2nd and 7th helps us to understand the working mechanism of xamarin.android.

Xamarin.android Architecture

The Java-written Android application implements some of the system's functions by calling android.* and Java.* classes under these namespaces, such as sound, display, OpenGL, and other functions that are not possible through the Java API or that are related to hardware and system platforms. So the question here is how do these functions get called when we write in C #? How is the Android application written in C # initialized?

Android callable Wrappers (ACW)

When a C # developed Android program runs, in addition to a Dalvik virtual machine instance, there is also a mono virtual machine instance running. The Dalvik virtual machine entity is like a host, our app runs on the host, and all of our methods written in C # are called in the form of ACW. Invoke the native in Java code just as you would invoke other C or C + + code.

Momodroid.exe will generate the corresponding ACW for our C # class during the compile phase.

1 usingSystem;2 usingAndroid.app;3 usingAndroid.os;4 5 namespaceMono.Samples.HelloWorld6 {7          Public classhelloandroid:activity8         {9                 protected Override voidOnCreate (Bundle savedinstancestate)Ten                 { One                         Base. OnCreate (savedinstancestate); A Setcontentview (r.layout.main); -                 } -         } the}

Corresponds to the generated ACW code:

1  PackageMono.samples.helloWorld;2 3  Public classhelloandroid4         extendsandroid.app.Activity5 {6         Static FinalString __md_methods;7         Static {8__md_methods =9"N_oncreate: (landroid/os/bundle;) v:getoncreate_landroid_os_bundle_handler\n" +Ten""; OneMono.android.Runtime.register ("Mono.Samples.HelloWorld.HelloAndroid, HelloWorld, version=1.0.0.0, culture=neutral , Publickeytoken=null ", helloandroid.class, __md_methods); A         } -  -          Publichelloandroid () the         { -                 Super (); -                 if(getclass () = = Helloandroid.class) -Mono.android.TypeManager.Activate ("Mono.Samples.HelloWorld.HelloAndroid, HelloWorld, version=1.0.0.0, culture= Neutral, Publickeytoken=null "," ", This,Newjava.lang.object[] {}); +         } -  + @Override A          Public voidonCreate (android.os.Bundle p0) at         { - n_oncreate (p0); -         } -  -         Private native voidn_oncreate (Android.os.Bundle p0); -}

You can see that the native N_oncreate method above will call the Mono.Samples.HelloWorld.OnCreate method that was registered with the mono virtual machine.

Managed callable Wrappers (MCW)

Above we talk about the Dalvik virtual machine is the host of our C # developed Android application, which uses ACW to invoke the methods we write in C #. If our method in C # involves some sound or system itself, or now there are many mature Java-written libraries, what do we want to call? The answer is we'll go back through MCW. Calling Java in the C # runtime is called MCW.

A lot of the xamarin.android framework is MCW, about the creation of MCW and the development of C # and Java integration, and we'll talk a little more about how a C # developed Android app was launched, and we'll talk about it later. For Android applications developed by C #, the performance will certainly have an impact, but the impact is how much, I have not done a specific test, so it is not detailed. In the back we carefully to contrast.

Xamarin.ios Architecture

For developers, Xamarin.ios is a lot simpler 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 native machine code directly into Iphonee. That means the iphone app written by C # is the same as objective-c. And the Xamarin team is guaranteed to update Xamarin.ios every time the iOS system is updated so we don't have to worry about being delayed.

Summary

This is just a rough introduction to how Xamarin mobile cross-platform scenarios work, and there's too much we don't involve. Xamarn.forms provides a unified UI for a wide variety of mobile platforms (although there are some special implementations that are not implemented yet, the official does not push it to make prototypes, or proof of concept can be tried, and if your UI is really simple, that's fine.) , of course, when Xamarin.Forms doesn't meet your requirements, you can use Xamarin.android and Xamarin.ios UI controls, which are also native, meaning there's no performance impact on the UI.

For now, it's more advantageous to develop an iOS app in C #, and the app that we're developing for the iphone in VS needs to be compiled with a Mac. In contrast, the development of Android applications will appear cumbersome, there are some hidden things we do not specifically, including the creation of an app, C # and Java integration in the development of the interaction and so on, these questions we stay behind slowly to solve.

Reference reading

Http://developer.xamarin.com/guides/android/under_the_hood/architecture/
Http://en.wikipedia.org/wiki/Java_Native_Interface
http://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/

C # Mobile cross-platform development (2) How does Xamarin mobile cross-platform solution work?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.