Monday, March 6, 2017

Start development with Xamarin.Forms



What is Xamarin? >>



Structure of Applications


The Collective objects which are appears  in the Xamarin.Forms is known as Visual elements. There are main 03 Categories.

  • Layout
  • Page
  • Views



Xamarin.Forms  API defines classes named VisualElement, Page, Layout, and View. These classes and descendants of those classes form the backbone of Xamarin.Forms  user –interface. VisualElement is important class in the Xamarin.Forms. A Visual-element object is anything that occupies space in the screen.
Xamarin.Forms application consists of one page or multiple pages. Pages occupies all of the screen. Some applications consists of one page and other pages allow to navigate in multiple pages.

On each page visual elements arrange in parent- child heirachy. Some layouts have a single child, but many layouts have multiple children that the layout arranges within itself. These children can be other layouts or views. Different types of layouts arrange children in a stack, in a two-dimensional grid, or in a more freeform manner.
The views in Xamarin.Forms familiar types of presentations and objects such as : text, bitmaps, buttons, text-entry fields, sliders, switches, progress bars, date and time pickers, and etc. These are normally known as widgets or controls in in the programming environement.


Now let see how to create new application using Visual Studio and Xamarin Studio.
In Visual Studio, select the menu option File > New > Project. At the left of the New Project dialog, select Visual C# and then Cross-Platform. In the center part of the dialog you’ll see several available solution templates, including three for Xamarin.Forms:

·  Blank App (Xamarin.Forms Portable)
·  Blank App (Xamarin.Forms Shared)
·  Class Library (Xamarin.forms)

To create a new Xamarin.Forms solution in Xamarin Studio, select File > New > Solution from the menu, and at the left of the New Project dialog, under Multiplatform select App, pick Forms App, and press the Next button. Toward the bottom of the next screen are a pair of radio buttons labeled Shared Code. These buttons allow you to choose one of the following options:

  • Use Portable Class Library
  • Use Shared Library



Portable in this refers to Portable Class Library (PCL) .In here all the common application code become a dynamic – link –library that can referred by all the individual platforms.

Shared in here means the Shared  Asset Project (SAP) it contain the loose code that are shared among the platform projects and it become a essential part of platform projects.
Select  Blank App (Xamarin.Forms Portable) in Visual Studio and Portable Class Library in Xamarin studio. And after that give project name then select the disk location that in the dialog in Visual Studio or the dialog which appears after clicking the next button in Xamarin studio.

If the project is run in Visual Studio there are six project running and one of them are common project rest five are application projects. 
Lets assume that our project name is Hello;

·        An Application project for Android is Hello.Droid.
·        An Application project for ios is Hello.ios.
·        An Application project for  Universal Windows Platform of Windows 10 and Windows Mobile 10 is Hello.UWP.
·        An Application project for Windows 8.1 is Hello.Windows.
·        An Application project for Windows phone 8.1 is Hello.WinPhone.


When you create the Xamarin.Forms solutions Xamarin.Forms  libraries  are automatically downloaded from the  NuGet package manager.

Visual Studio and Xamarin Studio store these libraries in the directory called Package In Visual Studio, in the Solution Explorer at the far right of the screen, right-click the solution name and select Manage NuGet Packages for Solution. The dialog that appears contains electable items at the upper left that let you see what NuGet packages are installed in the solution and let you install others. You can also select the Update item to update the Xamarin.Forms library. 

In Xamarin.Studio, you can select the tool icon to the right of the solution name in the Solution list and select Update NuGet Packages





Checking the Project Configuration

Before you continue your development check whether the configuration is done in correct manner . To check that in Visual Studio :

·        Select the Build   à Configuration Manager menu item.
·        In the Configuration Manager dialog, you’ll see the PCL project and the five application projects.
·        Make sure the Build box is checked for all the projects and the Deploy box is checked for all the application projects.
·        Take note of the Platform column: If the Hello project is listed, it should be flagged as Any CPU. The Hello.Droid project should also be flagged as Any CPU.
·        For the Hello.iOS project, choose either iPhone or iPhoneSimulator depending on how you’ll be testing the program.
·        For the Hello.UWP project, the project configuration must be x86 for deploying to the Windows desktop or an on-screen emulator, and ARM for deploying to a phone. 
 
If the project in Visual Studio is not compiling or deploying you must have to check the settings in the Configuration Manager dialog.May be some invalid Configuration become active or PCL project may  be not included.

In Xamarin Studio you can deploying to the iphone or iphone simulator through Project à Active Configuration menu item.

In Visual Studio you have to show the tool bar in ios and android.these tool bar can be select from the  among emulators and devices and allow you to manage the emulators. And make sure wheather  you has been check these items :

·        Views à Toolbars à ios
·        Views à Toolbar à  Android
                         
In Visual Studio their  is Solution Explore, right-click any of the five application projects and select the Set As StartUp Project item from the menu. You can then select to deploy to either an emulator or a real device. To build and run the program, select the menu item Debug à Start Debugging.
In the Solution list in Xamarin Studio, click the little tool icon that appears to the right of a selected project and select Set As Startup Project from the menu. You can then pick Run à Start Debugging from the main menu.

If all processes taken places in correct  manner skeleton application will be created in the template will be run and it will be display a message.
There are different color schema for different platform in iOS and  Windows ark there is text in the light background  in Android light color text in the dark background. Windows 8.1 and Windows Phone 8.1 have same as in the Android.


Inside the Files

The program created inside the Xamarine.Forms is very simple as a result we have opportunity to understand the code files and interrelationship of each other and how they work.
Lets try to understand the code which is related to things that we are seen in the screen.This is know as App class in the HelloProject we created.If the project is created in the Visual Studio  App class is defined in the App.cs file but in Xamarin Studio it is Hello.cs file.

using System;
using System.Collections.Generic;
using System.Linq; using System.Text; 
using Xamarin.Forms; 

namespace Hello{  
 public class App : Application{       
 public App(){           
 // The root page of your application           
            MainPage = new ContentPage{            
       Content = new StackLayout{                
            VerticalOptions =LayoutOptions.Center,                  
 Children = {                        
 new Label {                           
 HorizontalTextAlignment =                                TextAlignment.Center,Text = "Welcome to Xamarin Forms!"
     }
    }
   }         
  };     
 } 

    protected override void OnStart()      
  {          
  // Handle when your app starts      
  } 

    protected override void OnSleep()      
  {    
      // Handle when your app sleeps      
  }

    protected override void OnResume()
  {          
      // Handle when your app resumes
  }      
}
}

The code that the Xamarin.Forms template has generated here shows one very simple approach to defining this constructor: The ContentPage class derives from Page and is very common in singlepage Xamarin.Forms applications. It occupies most of the phone’s screen with the exception of the status bar at the top of the Android screen, the buttons on the bottom of the Android screen, and the status bar at the top of the Windows Phone screen.

The ContentPage class defines a property named Content that you set to the content of the page. Generally this content is a layout that in turn contains a bunch of views, and in this case it’s set to a StackLayout, which arranges its children in a stack. 
This StackLayout has only one child, which is a Label. The Label class derives from View and is used in Xamarin.Forms applications to display up to a paragraph of text.
For your own single-page Xamarin.Forms applications, you’ll generally be defining your own class that derives from ContentPage. The constructor of the App class then sets an instance of the class that you define to its MainPage property.



The iOS Project


The ios project contain the classes that are derive from the UIApplicationDeligate (A class used to receive events raised by a UIApplication.)
FormsApplicationDelegate this is a alternative class which is define by Xamarin.Forms.Platform.iOS library.
And there is  AppDelegate.cs file in Hello.ios project which contain all using using directives and comments.

using Foundation;
using UIKit; 
namespace Hello.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);      
  }   
 }
 }


The Android Project

In the Android application, the typical MainActivity class must be derived from a Xamarin.Forms class named FormsApplicationActivity, defined in the Xamarin.Forms.Platform.Android assembly, and the Forms.Init call requires some additional information:

using Android.App;
using Android.Content.PM;
using Android.OS; 
namespace Hello.Droid
 {   
 [Activity(Label = "Hello", 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());      
  }   
 }
 }



The Windows Project

In the UWP project (Universal Windows Project), look first in the App.xaml.cs file tucked underneath the App.xaml file in the project file list. In the OnLaunched method you will see the call to Forms.Init using the event arguments:
Xamarin.Forms.Forms.Init(e);
Now look at the MainPage.xaml.cs file tucked underneath the MainPage.xaml file in the project file list. This file defines the customary MainPage class, but it actually derives from a Xamarin.Forms class specified as the root element in the MainPage.xaml file. A newly instantiated App class is passed to the LoadApplication method defined by this base class:

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



PCL or SAP






When you create your first application in Visual Studio there are 02 template to be selected:
·        Blank App (Xamarin.Forms Portable)
·        Blank App (Xamarin.Forms Shared)






In Xamarin Studio :
·        Use Portable Class Library
·        Use Shared Library

In Xamarin Studio first option is to create a  portable class library and then create a Shared Asset Project which only create the shared code. Original application solution consist the PCL template.And now we create the second solution (Assume that the application that we make is Hello) as HelloSAP with the SAP template.
After you create all these things all are look like same but there is one extra item in in HelloSAP that is App.cs.file.

With both the PCL and SAP approaches, code is shared among the five applications, but in decidedly different ways: With the PCL approach, all the common code is bundled into a dynamic-link library that each application project references and binds to at run time.





With the SAP approach, the common code files are effectively included with each of the five application projects at build time. By default, the SAP has only a single file named App.cs, but effectively it’s as if this HelloSap project did not exist and instead there were five different copies of this file in the five application projects.
iOS and the Android have the some what same features of the .Net but it not 100% equal to the .Net  that the Windows used. This means that any .Net classes accessed by the shared code might be somewhat different depending on the platform.
In the projects generated by the Xamarin.Forms template, the various application projects define symbols that you can use with these directives.





No comments:

Post a Comment