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.





Tuesday, February 28, 2017

Microsoft Xamarin

What is Xamarin?

What  is  Xamarin ?

Xamarin is a tool which used to develop the cross platform. It mishap the problems that face by developers when developing the cross platform apps.


Histoty of Xamarin


The main problems faces by developers with out cross platforms are:

Different user interface paradigms
In these days there are two major phones and tabs platform dominate phones and iPads which are run in ios operating system and varieties of phone and tabs which are run on android operating system. There is another mobile platform which not so popular as ios and android that is Microsoft’s Windows  phones. Now a day’s Software developer’s optimum strategy is to develop in more than one platform. But due to different user interface paradigms it becomes difficult. Navigation throughout the application and pages, different ways of presenting data ,different ways of displaying menu are different in each mobile platforms.

Different development environment
Their different IDE for all these mobile platforms and there are different from each other. Xcode on Mac for develop the ios ,  Android  studio for develop the android , Visual studio for develop the windows.

Different programming interface
In many cases, the three platforms all implement similar types of user-interface objects but with different names.

Different programming languages
Programmers have some kind of flexibility to chosen the programming language to three platforms. But in generally each platform is closely associated with particular programming language. Objective C for ios, Java for android , C # for windows.

Due to Xamarin overcome those problems Xamarin become the most suitable tool for developing the mobile apps. In here you can use C# for ios,Android, Universal Windows apps. And interface design for all three platforms can be accomplished within the XAML based framework in Xamarin forms.
Structure of a Xamarin APP

Why  is  Xamarin ?

Native Application
Native design is preferred solution for any application.Xamarine studio enables the code completion in C#. It provide the advantages of native UI  and  access to specific device features .Code sharing across platform is a breeze with Xamarin and it shorten the development cycle.

Easy collaboration and sharing the app logic
This is one main reason why Xamarin become must use cross platform development tool for developing the mobile apps. Application logic underlying the UI layer, like input validation, web service calls , database interaction  and back-end enterprise integrations are coded once in C#. 75% of code can be shared across various operating systems. This saves the time.





Seamless API Integration Capability
In here code can be shared between ios , Android  and Windows using Portable Class Libraries(PCL)and appropriate application architecture with Xamarin . Their unique binding technology enables them to provide support for new features soon after they introduce in the device’s operating system.

Xamarin Component Store
Developers can choose from host of free or paid components which include UI controls, cross platforms libraries, and third party web services to apps with just a few lines of code. The component Store is built right into Xamarin studio and Xamarin’s Visual Studio extension.


Read More About Xamarin

Why Xamarin.Forms is more better than Xamarin?

Different between Xamarin and Xamarin.Forms

Xamarin

  • Create one UI per  platforms.
  • Cross platform development process is much slower.
  • Need to learn all native UI frameworks.
  • Having duplicate codes.


Xamarin.Forms 

  • Create one UI for all platforms.
  • Cross platform development process is  fast.
  • No need to learn all native UI frameworks.
  • Use basic components which are available in platforms.

Due to these reason using Xamarine.Forms rather than using Xamarin is more effective in developments.




Introducing Xamarin.forms




Xamarin introduce Xamarin.forms which allows you to write user-interface code that can be compiled in iOS, Android  and Windows platforms.

Xamarin.forms Supports five distinct platforms :



· iOS for programs that run on the iPhone, iPad, and iPod Touch.

· Android for programs that run on Android phones and tablets.

· The Universal Windows Platform (UWP) for applications that runs under Windows 10 or Windows 10 Mobile.

· The Windows Runtime API of Windows 8.1

· The Windows Runtime API of Windows Phone 8.1



Generally Xamarin.forms  application in Visual Studio consists of five projects for each of five platforms there is another project which contain common code. But the five of these platforms are typically quite it consists just little startup code.

The PCL (Portable Class Library) or SAP(Shared Asset Project ) consist of bulk of application including the user-interface  code.

Xamarine.Forms.Core and  Xamarin.Forms.Xaml libraries implement the Xamarin . Forms API. Depending on the platform, Xamarin.Forms. Core then makes use of one of the Xamarin.Forms.Platform libraries. These libraries are mostly a collection of classes called renders that transform the Xamarin.Forms user-interface objects into the platform-specific user interface


Structure of Xamarin code



Development  Environment of Xamarin.


Machines and related IDEs

Xamarin development environment


If you’re going to develop in iPhones you need a Mac for this Purpose and after that you want to install Xcode in Mac and, of course, the Xamarin platform that includes the necessary libraries and Xamarin Studio. You can use Xamarine studio and Xamarin.Forms in development of iOS application in Mac.

Once you install the Xcode and Xamarin platform in Mac you can also installed Xamarine platform to the PC and programe to the iPhones using Visual Studio.

The PC and Mac should be connected through a network (Like Wi-Fi). Visual –Studio Communicate with Mac through a Secure Shell interface and use Mac to build the application and run the program.

Android programming also can be done in both Xamarin Studio on Mac or Visual Studio on PC.


When you installing Visual Studio 2015 > Custom > Select all following parts

Install step one

Install step two

Install step three

If you want to target the Windows platforms, you’ll need Visual Studio 2015. You can target all the platforms in a single IDE by running Visual Studio 2015 on a PC connected to the Mac via a network.   




Devices and Emulators

In here we have capability to test our application on real phone connected to the machine using USB cable or can test the application through the emulator in the screen.

There are advantages and disadvantages in these testing methods. Emulator give us a idea about the size of  application and hoe the different forms are formed and a real phone testing is needed to check the complex touch interaction or response time.
When we consider the Mac those machines do not have the touch gesture, so you have to use mouse to simulate the touch. You also can connect a real iPhone to the device and check but you want to make it as a developer device.

Device and Emulators


Android emulators supplied by Google have tended to be slow and cranky, although they are often extremely versatile in emulating a vast array of actual Android devices. Fortunately,  Visual Studio now has its own Android emulator that works rather better. It’s also very easy to connect a real Android phone to either a Mac or PC for testing. All you really need do is enable USB Debugging on the device.

The Windows Phone emulators are capable of several different screen resolutions and also tend to run fairly smoothly, albeit consuming lots of memory. If you run the Windows Phone emulator on a touchscreen, you can use touch on the emulator screen. Connecting a real Windows Phone to the PC is fairly easy but requires enabling the phone in the Settings section for developing. If you want to unlock more than one phone, you’ll need a developer account. 

Installation

Before you writing the application Xamarin.forms you have to install the Xamarin platform to Mac , PC or both. After installing the platform your eager to create the application but before that you have to try to creating Xamarin projects for the ios , Android and Windows.
If you experience problem in iOS Xamarin, Xamarin Android and Xamarin Windows that is a not a problem of Xamarin.Forms before using the Xamarine.Forms  you have to solve that Problem.

Xamarin for MAC & Windows <<  (Click here)
 Xamarin for windows

 Xamarin for mac

















Creating an iOS app

If you’re using Visual Studio, and if everything is installed correctly, you should be able to select

File > New > Project from the menu, and in the New Project dialog, from the left, select Visual C# and iOS and then Universal (which refers to targeting both iPhone and iPad), and from the template list in the center, select Blank App (iOS).

If you’re using Xamarin Studio, you should be able to select File > New > Solution from the menu, and in the New Project dialog, from the left, select iOS and then App, and from the template list in the center, select Single View App.

Creating iOS App



Creating an Android app

If you’re using Visual Studio, and if everything is installed correctly, you should be able to 

Select File > New > Project from the menu, and in the New Project dialog, from the left, select Visual C# and then Android, and from the template list in the center, select Blank App (Android).

If you’re using Xamarin Studio, you should be able to select File > New > Solution from the menu, and in the New Project dialog, from the left, select Android and App, and in the template list in the center, select Android App.

Give it a location and a name; build and deploy. If you can’t get this process to work, it’s not a Xamarin.Forms issue, and you might want to check the Xamarin.Android forums for a similar problem.

Creating Android App



Creating a Windows App

In Visual Studio 2015, if everything is installed correctly, you should be able 

Select File > New > Project from the menu, and in the New Project dialog, at the left, select Visual C# and Windows.


Creating Windows App


Start Development using Xamarin.Forms