Xamarin. Forms cross-platform development-Part 2: in-depth analysis,

Source: Internet
Author: User

Xamarin. Forms cross-platform development-Part 2: in-depth analysis,
Original English:

Https://developer.xamarin.com/guides/xamarin-forms/getting-started/hello-xamarin-forms/deepdive /#

 

The first part of this article establishes the Phoneword application. This article is the second part. It reviews the construction content to understand the basic working principles of the Xamarin. Forms Application.

 

We will discuss the following questions:

  • Visual Studio Introduction: Create a New Xamarin. Forms Application Introduction.
  • Analyzes the Xamarin. Forms Application and introduces the basic components of the application.
  • Architecture and application basics-how can an application be started on each platform.
  • Create a user interface in the Xamarin. Forms Application.
  • Some additional concepts involved in Phoneword.
  • Test and release-suggestions for testing, releasing, and generating works.

 

Introduction to Visual Studio

Visual Studio is a powerful IDE developed by Microsoft. It fully integrates the visual designer, text editor, optimization refactoring tool, package manager, and source code integration. This article describes some basic features related to the Xamarin plug-in.

Visual Studio organizes code into solutions and projects. A solution is a container that can accommodate one or more projects. It can be an application project that supports database projects, test projects, or other projects. The Phoneword application contains a solution containing six projects, such.

 

These projects are:

Phoneword-this project is a portable class library project. All the shared code and shared UI are in it.

Phoneword. Droid-Android code and Android app portal.

Phoneword. IOS-code for the IOS system and IOS app Portal

Phoneword. UWP-code for the Windows universal platform (UWP) system, and the portal to the application of this Platform

Phoneword. WinPhone-contains the code for the dedicated needle and Windows Phone platform, and the portal for the Windows Phone 8.0 application.

Phoneword. WinPhone81-contains the code for the special needle and Windows Phone8.1 platform, and the entry for the Windows Phone 8.1 application.

 

Analysis of Xamarin. Forms applications.

Displays the Phoneword PCL project in Visual Studio.

 

 

 

 

This project contains two folders:

Reference-contains the library files required for building and running the application.

Properties-including AssemblyInfo. cs, he is. NET Library metadata file, which contains some basic information about the application. This is a good habit. For more information about this file, see the AssemblyInfo class on MSDN.

 

The project also contains some files:

The tag file corresponding to the App. xaml-XAML App class defines the resource dictionary for the application.

The code file of the App. xaml. cs-App class contains the initialization and display of the first page. It also controls the application lifecycle events.

The IDialer. cs-IDialer interface specifies the Dial method that must be implemented in the implementation class.

The xaml Tag file of the MainPage. XAML-MainPage class. Defines the page elements (UI) when the application is started ).

MainPage. xaml. cs-MainPagel class code. Contains the business logic for user-interface interaction.

Packages. config-an XML file that contains information about the NuGet package, used to track required package files and corresponding versions. Both Xamarin Studio and Visual Studio can be configured to automatically restore missing Nuget packages. When you share code with other programmers, the content contained in this file is controlled by the NuGet manager.

PhoneTranslator. cs-the business logic for converting phone words into phone numbers, which is used by MainPage. xaml. cs.

 

For more information about Xamarin. IOS app profiling, see https://developer.xamarin.com/guides/ios/getting_started/hello,_ios/hello,_ios_deepdive#Anatomy_of_a_Xamarin.iOS_Application/

For more information about the Xamarin. Android Application, see

Https://developer.xamarin.com/guides/android/getting_started/hello,android/hello,android_deepdive#Anatomy_of_a_Xamarin.Android_Application/

 

Architecture and application infrastructure

Similar to traditional cross-platform applications, Xamarin. the Forms Application puts the shared code into the portable class library (PCL). The platform-related applications consume the shared code and demonstrate the relationship between each part of the Phoneword application:

 

 

For more information about PCL, see

Https://developer.xamarin.com/guides/cross-platform/application_fundamentals/pcl/introduction_to_portable_class_libraries/

To maximize reuse of startup code, the Xamarin. Forms Application has a separate class called App, which initializes the first page of each platform, as shown in the following code:

 

using Xamarin.Forms;using Xamarin.Forms.Xaml; [assembly: XamlCompilation(XamlCompilationOptions.Compile)]namespace Phoneword{    public partial class App : Application    {        public App()        {            InitializeComponent();            MainPage = new MainPage();        }        ...    }}

 

 

This Code assigns a new Mainpage instance to the MainPage attribute of the App. The XamlCompilation attribute enables the XAML compiler to compile the XAML into an intermediate language. For more information about XAML, see

Https://developer.xamarin.com/guides/xamarin-forms/xaml/xamlc/

 

Start applications on various platforms

IOS

To execute the Xamarin. Forms page on IOS, The Phoneword. IOS project contains the AppDelegate class inherited from FormsApplicationDelegate, as shown in the following code:

 

namespace Phoneword.iOS{    [Register ("AppDelegate")]    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate    {        public override bool FinishedLaunching (UIApplication app, NSDictionary options)        {            global::Xamarin.Forms.Forms.Init ();            LoadApplication (new App ());            return base.FinishedLaunching (app, options);        }    }}

 

 

By calling the Init method, the FinishedLaunching method overwrites the process of initializing the Xamarin. Forms framework. This allows the IOS platform to call the LoadApplication method so that Xamarin. Forms is called before the Root View Controller is set.

 

Android

To start the Xamarin. forms page on Android, The Phoneword. Droid project contains the code for creating an Activity with the MainLauncher attribute, which is integrated from the formsApplicationActivity class. As follows:

 

namespace Phoneword.Droid{    [Activity (Label = "Phoneword.Droid",               Icon = "@drawable/icon",               MainLauncher = true,               ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity    {        protected override void OnCreate (Bundle bundle)        {            base.OnCreate (bundle);            global::Xamarin.Forms.Forms.Init (this, bundle);            LoadApplication (new App ());        }    }}

 

 

By calling the Init method, the Oncreate method overwrites the initialization process of Xamarin. form. On the Android platform, Xamarin. Forms is loaded before the application is loaded.

 

Universal Windows platform (UWP)

 

In general Windows applications, the Init Method for initializing the Xamarin. Forms framework is started in the App class.

Xamarin.Forms.Forms.Init (e);if (e.PreviousExecutionState == ApplicationExecutionState.Terminated){  ...}

 

This allows the implementation of Xamarin. Forms on the UWP platform to be loaded in the application. The initial Xamarin. Forms page is loaded by the MainPage class, as shown in the following code.

namespace Phoneword.UWP{    public sealed partial class MainPage    {        public MainPage()        {            this.InitializeComponent();            this.LoadApplication(new Phoneword.App());        }    }}

 

The Xamarin. Forms Application is loaded together with the LoadApplication method.

 

User Interface (UI)

In the Xamarin. Forms Application, there are four main control groups used to create user interfaces.

1. pages-Xamarin. the Forms page displays the screen of a cross-platform mobile app. The Phoneword app uses the ContentPage class to display a single screen. For more information about the page, see Xamarin. formsPages (https://developer.xamarin.com/guides/xamarin-forms/controls/pages)

2. Layouts-Layouts is a container used to construct the logical structure of a view. Phoneword uses the StackLayout class to arrange controls to a horizontal stack.

Related Article

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.