Unit 5
Unit 5
Xamarin:
Writing code once and using it on multiple platforms has been a dream of many software developers.
Although this has been possible for some time now, it always came at the cost of maintainability, ease of
testing, or even worse, poor user experience.
Now Xamarin is available
Xamarin can share code across multiple platforms without compromising any of the other aspects of
mobile application development. By this way, Android, Windows and iOS in particular can get support
from Xamarin platform.
What Is Xamarin?
Xamarin is a development platform that allows you to write cross-platform—yet native—applications for
iOS, Android, and Windows Phone in C# and .NET.
XAML
XAML—the eXtensible Application Markup Language—allows developers to define user interfaces in
Xamarin.Forms applications using markup rather than code. XAML is never required in a Xamarin.Forms
program, but it is often more succinct and more visually coherent than equivalent code, and potentially
toolable. XAML is particularly well suited for use with the popular MVVM (Model-View-ViewModel)
application architecture: XAML defines the View that is linked to ViewModel code through XAML-based
data bindings.
XAML has several advantages over equivalent code:
XAML is often more succinct and readable than equivalent code.
The parent-child hierarchy inherent in XML allows XAML to mimic with greater visual clarity the
parent-child hierarchy of user-interface objects.
XAML can be easily hand-written by programmers, but also lends itself to be toolable and
generated by visual design tools.
Of course, there are also disadvantages, mostly related to limitations that are intrinsic to markup
languages:
XAML cannot contain code. All event handlers must be defined in a code file.
XAML cannot contain loops for repetitive processing. (However, several Xamarin.Forms visual
objects—most notably ListView —can generate multiple children based on the objects in
its ItemsSource collection.)
XAML cannot contain conditional processing (However, a data-binding can reference a code-
based binding converter that effectively allows some conditional processing.)
XAML generally cannot instantiate classes that do not define a parameterless constructor.
(However, there is sometimes a way around this restriction.)
XAML generally cannot call methods. (Again, this restriction can sometimes be overcome.)
There is not yet a visual designer for generating XAML in Xamarin.Forms applications. All XAML must be
hand-written, but there is a XAML Previewer. Programmers new to XAML might want to frequently build
and run their applications, particularly after anything that might not be obviously correct. Even
developers with lots of experience in XAML know that experimentation is rewarding.
XAML is basically XML, but XAML has some unique syntax features. The most important are:
Property elements
Attached properties
Markup extensions
Xamarin Forms
Xamarin.Forms is a layer on top of the other UI bindings and the Windows Phone API, which provides a
completely cross-platform user interface library.
Xamarin provides C# bindings to native Android and iOS APIs. This gives you the power to use all of
Android and iOS’ native user interface, notifications, graphics, animation, and other phone features—all
using C#.
Each new release of Android and iOS is matched by Xamarin, with a new release that includes bindings
for their new APIs.
Xamarin’s port of .NET includes features such as data types, generics, garbage collection, language-
integrated query (LINQ), asynchronous programming patterns, delegates, and a subset of Windows
Communication Foundation (WCF). Libraries are managed with a linger to include only the referenced
components.
Writing Cross-platform Applications
In order to write cross-platform applications with Xamarin, developers need to choose one of the two
available types of projects:
Portable Class Library (PCL)
Shared Project
The PCL allows you to write code that can be shared among multiple platforms, but with one limitation.
Since not all .NET APIs are available on all platforms, with a PCL project, you will be limiting it to run on
platforms for which it is targeted.
During the build process, a PCL is compiled into separate DLLs and loaded by Mono during runtime. A
different implementation of the same interface can be provided during runtime.
On the other hand, shared projects give you more control by allowing you to write platform-specific
code for each platform you want to support. The code in a shared project can contain compiler
directives that will enable or disable sections of code depending on which application project is using
the code.
Unlike a PCL, a shared project does not produce any DLL. The code is included directly in the final
project.
Build your first Xamarin.Forms App (Xamarin.Windows)
Follow these steps along with the video above:
1. Choose File > New > Project... or press the Create new project... button, then select Visual C# >
Cross-Platform > Mobile App (Xamarin.Forms):
2. Ensure Android and iOS are selected, with .NET Standard code sharing:
3. Wait until the NuGet packages are restored (a "Restore completed" message will appear in the
status bar).
4. Launch Android emulator by pressing the debug button (or the Debug > Start Debugging menu
item).
5. Edit MainPage.xaml, adding this XAML before the end of the </StackLayout>:
<Button Text="Click Me" Clicked="Button_Clicked" />
6. Edit MainPage.xaml.cs, adding this code to the end of the class:
int count = 0;
void Button_Clicked(object sender, System.EventArgs e)
{
count++;
((Button)sender).Text = $"You clicked {count} times.";
}
7. Debug the app on Android:
XAMARIN.IOS
1. Create a new Xamarin.iOS solution by selecting File > New > Project... > Visual C# > iPhone &
iPad > iOS App (Xamari:
In the next dialog that appears, select the Single View App template and press OK to create the
project:
2. Confirm that the Xamarin Mac Agent icon in the toolbar is green.
If it isn't, this means that there is no connection to your Mac build host, follow the steps in
the configuration guide to get connected.
3. Open the Main.storyboard file in the iOS Designer by double-clicking on it in the Solution
Explorer:
4. Open the Toolbox tab, type “label” into the search bar and drag a Label onto the design surface
(the area in the center):
5. Next, grab the handles of the Dragging Controls and make the label wider:
6. Similarly any control can be created and implemented in app.