Preface
The pace of application development has never stopped. From early Windows applications, to Web applications in the Web era, to mobile application, the most popular mobile app in recent years, the developer of C # and the consumer of Visual Studio. Is the development of Windows Phone and Windows Store app not enough for you? If you are able to use C # and visual Studio to develop iOS and Android apps. Is it possible to awaken your development soul again? Xamain is the framework that was born to this demand.
Introduction
Xamarin was evolved from an early mono project and was originally an open source code project for executing C # programs on Linux. The Mono Touch framework supporting iOS and the mono for Android framework have been published. So it's not a new product on the market. In 2011, the original Mono project team at Novell set up a company to formally change its name to Xamarin, which is the origin of Xamarin. Xamarin has the following characteristics:
- quickly build native (Native) mobile device applications -with C # affinity and full-featured language features, with the convenience of the. NET Framework's base Class Library (BCL). Enables developers to develop mobile device applications with the highest productivity. Currently Mono supports the C # 5.0 language Specification, which means that developers can use your familiar lambda Expression, LINQ, or even async/await for non-synchronous processing.
- program code is shared -existing iOS applications developed by OBJECTIVE-C and Android applications developed by Java cannot be shared with program code. Xamarin is written in C #, so through good design, you can share business logic and data access programs across different platforms without having to re-write. In addition to saving the time cost of re-writing, there is considerable benefit for version maintenance and consistency.
- integration with Visual Studio –xamarin provides Visual Studio 2010/2012 's plug-in so that developers who are already familiar with Visual Studio are no longer familiar with other development tools. When you create a project, you can create a project template for iOS and Android directly. Of course, the development team can also incorporate iOS and Android program code into ALM, using Team Foundation Server for version and build management.
- make sure to update –xamarin for iOS and Android version updates, in iOS SDK 5.0, 6.0 6.1, with Apple on the same day as the corresponding framework version, 7.0 also promised to be published on the same day. On the Android side, Xamarin even changed the original Android code from Java to C #, and the performance has been greatly improved.
- Native Application Performance –xamarin development compilers for iOS and Android platforms. Compile the C # program code directly into the machine code of the ARM CPU. As a result, the performance will be no different than the native mode development application. On Android, because Xamarin does not use Android's Dalvik virtual machine, it uses the CLR to test its performance more than its native application.
System Requirements
- iOS: because Apple restricts IOS app compilation to be through Xcode, it requires 1 Mac machines as the build Host.
- Windows 7 or the updated job system version
- Visual Studio 2010/2012
- OS X Lion or the updated job system version
- Xcode IDE and IOS SDK
- android: for Android Development, it can be done entirely on Windows. The system requirements are as follows:
- Windows 7 or the updated job system version
- Java SDK
- Android SDK
- Xamarin.android for Visual Studio
Install Xamarin
Whether it's easy to install Xamarin on Windows or Mac, just fill in the name and email account on the http://xamarin.com/download to download it. As long as the Installation Wizard to select the next step, you can install the Android SDK and other related components.
Windows Edition installation Screen:
Installation screen for Mac version:
Xamarin is a commercial framework, but you can try it for a full 30 days or use the free version. But the free version limits the developer's compiled IL code size to no more than 32K, and cannot be developed using visual Studio (only with Xamarin studio provided by Xamarin).
After you've installed Xamarin, you'll need to register a Xamarin account to start.
1. Open Visual Studio, click Tools =>xamarin Account
2. If you have previously registered a Xamarin account (please note that the Xamarin account is different from the email you previously downloaded), please enter your account password directly, and if you are using it for the first time, please apply for a new Xamarin account.
You can start developing Android and iOS apps when you log in!!
developing Android helloworld! with visual Studio
Using Xamarin to develop Android apps can be said to be quite handy, not only to design user interfaces in Visual Studio using drag and drop, but also to integrate tools such as AVD and SDK Manager. Needless to say, start our 1th Android app on Visual Studio ~
1. New project in Visual Studio, in the project template, expand the C # node, you will find that the project template will be more than the Android and iOS type of project template, we select "Android Application" in this example, and in the name enter " Hellodroid "(Of course you can change to any name you want)
2. After the project is established, you will find a few more items in the tool column:
The instructions are as follows (left to right):
- Target device: You can choose which device to deploy the current project to, and the preset "Prompt for device" will pop up the window for the user to choose when it starts debugging/deploying. If a physical device is connected, it will also appear in this menu.
- Device Record: You can select the device and display the active record of the current device.
- Opens the Android Virtual Appliance Administrator (AVD), which allows you to add, open or edit an Android emulator.
- Android SDK Administrator: Thanks to the numerous Android versions (API level), you can install the required SDK versions and components in this admin interface.
3. A button control is built into the preset project and the "Hello World, click me!" is written, and the Resources->layout->main.axml under the project can be designed for the page.
4. The main program code for this example is Activity1.cs, which is illustrated directly by the annotations:
See this program code, I think everyone should not feel strange. I have some students who tell me that it feels like developing a Windows Form application.
Activity1.cs is equivalent to the MVC layered controller. Tell the system which page to load with the Setcontentview method. Then the object entity of the button on the screen is obtained by Findviewbyid method. You can then compose related program code for the properties or methods of the button, or handle the event of the button. As shown, the default example is to use the anonymous method, which we annotate, and then use lambda expression to compose.
5. Then press F5, you can start the debugging, if the Android emulator is not pre-opened, then the running Devices list will be empty, you can click on the "Start emulator image" link below to launch the simulator.
6. The results of the implementation are as follows:
Isn't it quite simple? Next we'll start the iOS Hello world.
developing iOS helloworld! with visual Studio
Unlike the development of Android, Xamarin currently does not support the design of application pages in Visual Studio, but officials say they are currently in development and will provide this feature in a later version. So the current UI design for an iOS project has to be through the Mac's interface Builder (part of Xcode), or the location and size of the control is declared directly in the program, and HelloWorld in Visual Studio Application is using this method.
1. Add a project in Visual Studio, select iphone under iOS in the project template, and select HelloWorld application in the template on the right, then specify the project name (Helloios here)
2. When you first open an iOS project, Xamarin will prompt the consumer to develop an iOS project in Visual Studio only in a commercial version or later.
Then Xamarin will automatically enable the trial version of the authorization, after the launch, will prompt the consumer Visual Studio will be restarted.
If Xamarin licensing on your Mac has not been started, you will see a system prompt stating that build Server (that is, MAC) has not been enabled for authorization. Pressing the Activate button may not work at this time, follow the steps below to enable Xamarin licensing on your Mac.
3. Because Xamarin is preset on Mac, it is opened in free version. You can therefore force the Xamarin trial entitlement to be enabled by the following methods. Start Xamarin Studio on your Mac's finder
Add a new project and select iphone =>single View application. After the project is established, click Project =>profile-mono in the menu
You will see an authorization enabled screen. When this is done, we can go back to Visual Studio to specify our build Host.
4. In Visual Studio Tools/= option, you can see a more Xamarin node, click on the iOS Settings under Xamarin, and on the right you can specify the Mac Build Host. You can enter your Mac's computer name or IP address directly, Or press configure to set.
In the Settings window, Xamarin searches for Mac machines that have been installed and enabled for Xamarin in the same network segment. If the status is "ready", please press connect to connect.
If the status is not "ready", you can press the diagnose button to diagnose the connection. As shown, if one or more items cannot be diagnosed by wire, failed will appear, and you can expand the failed project to understand the cause of the failure.
5. In configuration management, set the platform to Iphonesimulator, which is scheduled to be deployed to the physical machine, but this requires a paid developer account from Apple.
Then in the tool column, you can choose the version of the iOS simulator, the right green tick symbol indicates that the connection with the Mac Build host is normal, if the anti-gray state, please click on the right side of the icon to re-connect with the specified Build host.
5. Open the MyViewConrtoller.cs document in the project. In this example, a button control (class name of UIButton in iOS) is also placed in the middle of the screen and the number of clicks is displayed. Since we are generating controls in the program, some of the program code is declaring the object's layout properties, please refer to the annotations in the program code:
6. The next step is to focus on the example program, and if you are designing the UI in Xcode, you can replace the program above.
Like the previous Android HelloWorld, in iOS, the button click event is not called Click, but touchupinside, so we through this event processing, after the user clicked the button, the display text of the Edge button ( Use UIButton's Settitle method). Finally, the button control is added to the screen through the View.addsubview method.
The results of the implementation are as follows:
Introduction to cross-platform development
As a cross-platform solution, using visual Studio with the Xamarin Development Action Appliance application has the following features:
- You can include projects for all platforms in the same scenario: Windows Phone, Windows 8 Store App, iOS, and Android. As shown in the following:
Program code sharing: At design time, the platform-independent business logic, service access, data access, and data layers are drawn out from the interface, as shown in:
The following shows how to achieve the above project integration and program code sharing, first of all to look at the architecture:
From the above, we can see that the left half can be drawn out of the common program code, this part will be independent into a project, and only add common components (such as the base Class Library), if some of the program code to use the platform-related components or technology, You can include conditional compilation in your program code for direct use by each platform, no modification is required.
In the project of each platform, the part of the common program code can be connected to each platform project through the way of file connection. However, since Visual Studio does not provide a way to set up multiple files or an entire folder connection at the same time, it is less practical to manually connect each file. And in the future, the common program code project if there is a file of the different, such as new or deleted. Each platform project does not synchronize updates. At this point we can assist with 1 extensions called "Project Linker 2012" (which also has a version corresponding to Visual Studio 2010).
Once the project linker is installed, we can click the right mouse button on each platform project to =>add Project link.
Then select the source project and then link to each folder and file for the source project.
The linked file will have a blue arrow icon in Visual Studio.
With the above settings, you can share the program code on each platform project. When you develop a platform project, you can simply compose a program for the user interface and for the manipulation of the control. For business logic, the data Access section can be shared across platforms, which is not achievable using native development methods (Objective-c, Java). The actual implementation results are as follows:
Conclusion
This article explains the features of Xamarin and the development architecture. It is hoped that the first contact developer will be able to start the installation and development immediately with a simple example. In the next series of articles, you'll learn how to use Xamarin and visual Studio to use iOS and Android's basic controls and multi-page threaded connection.
Visual Studio Cross-platform development (1): Hello xamarin!