Xamarin.Forms

Topics related to Xamarin.Forms:

Getting started with Xamarin.Forms

Xamarin.Forms makes it possible to create iOS, Android, and Windows apps with large amounts of shared code, including UI code or XAML UI markup. App pages and views are mapped to native controls on each platform, but can be customized to provide platform-specific UI or to access platform-specific features.

Navigation in Xamarin.Forms

Accessing native features with DependencyService

If you do not want your code to break when no implementation is found, check the DependencyService first if it has a implementation available.

You can do this by a simple check if it is not null.

var speaker = DependencyService.Get<ITextToSpeech>();

if (speaker != null)
{
    speaker.Speak("Ready for action!");
}

or, if your IDE supports C# 6, with null-conditional operator:

var speaker = DependencyService.Get<ITextToSpeech>();

speaker?.Speak("Ready for action!");

If you don't do this and no implementation is found at runtime, your code will generate an exception.

Navigation in Xamarin.Forms

The navigation on Xamarin.Forms is based on two principal navigation patterns: hierarchical and modal.

The hierarchical pattern allows the user to move down in a stack of pages and return pressing the "back"/"up" button.

The modal pattern is a interruption page that require a specific action from user, but normally can be canceled pressing the cancel button. Some examples are notifications, alerts, dialog boxes and register/edition pages.

DependencyService

When using DependencyService you typically need 3 parts:

  • Interface - This defines the functions you wish to abstract.
  • Platform implementation - A class within each platform specific project that implements the previously defined interface.
  • Registration - Each platform specific implementation class has to be registered with the DependencyService through a metadata attribute. This enables the DependencyService to find your implementation at run time.

When using DependencyService you are required to provide an implementation for each platform you target. When an implementation is not provided the application will fail at run time.

Custom Renderers

Unit Testing

Caching

Creating custom controls

Gestures

Data Binding

Possible Exceptions

System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.

This exception can occur when attempting to bind a collection to a non-bindable property when XAML pre-compilation is enabled. A common example is attempting to bind to Picker.Items. See below.

System.ArgumentException: Object of type 'Xamarin.Forms.Binding' cannot be converted to type 'System.String'.

This exception can occur when attempting to bind a collection to a non-bindable property when XAML pre-compilation is disabled. A common example is attempting to bind to Picker.Items. See below.

The Picker.Items Property Is Not Bindable

This code will cause an error:

<!-- BAD CODE: will cause an error -->
<Picker Items="{Binding MyViewModelItems}" SelectedIndex="0" />

The exception may be one of the following:

System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.

or

System.ArgumentException: Object of type 'Xamarin.Forms.Binding' cannot be converted to type 'System.String'.

Specifically, the Items property is non-bindable. Solutions include creating your own custom control or using a third-party control, such as BindablePicker from FreshEssentials. After installing the FreshEssentials NuGet package in your project, the package's BindablePicker control with a bindable ItemsSource property is available:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials"
             xmlns:my="clr-namespace:MyAssembly;assembly=MyAssembly"
             x:Class="MyNamespace.MyPage">
  <ContentPage.BindingContext>
    <my:MyViewModel />
  </ContentPage.BindingContext>
  <ContentPage.Content>
    <fe:BindablePicker ItemsSource="{Binding MyViewModelItems}" SelectedIndex="0" />
  </ContentPage.Content>
</ContentPage>

Working with Maps

Custom Fonts in Styles

Display Alert

Platform specific visual adjustments

Push Notifications

AppSettings Reader in Xamarin.Forms

Creating custom controls

Working with local databases

Push Notifications

AWS Simple Notification Service Lingo:

Endpoint - The endpoint can be a phone, email address or whatever, it's what AWS SNS can hit back with a notification

Topic - Essentially a group that contains all of your endpoints

Subscribe - You sign up your phone/client to receive notifcations

Generic Push Notification Lingo:

APNS - Apple Push Notification Service. Apple is the only one who can send push notifications. This is why we provision our app with the proper certificate. We provide AWS SNS the certificate that Apple provides us to authorize SNS to send a notification to APNS on our behalf.

GCM - Google Cloud Messaging is very similar to APNS. Google is the only one who can directly send push notifications. So we first register our App in GCM and hand over our token to AWS SNS. SNS handles all the complex stuff dealing with GCM and sending over the data.

Triggers & Behaviours

CarouselView - Pre-release version

CarouselView is a Xamarin Control which can contains any kind of View. This pre-release control can only be used in Xamarin Forms projects.

In the example provided by James Montemagno, on the blog of Xamarin, CarouselView is used to display images.

At this moment CarouselView is not integrated in Xamarin.Forms. To use this in your project(s), you will have to add the NuGet-Package (see example above).

Dependency Services

Access platform specific API from PCL or Shared project.

BDD Unit Testing in Xamarin.Forms

Xamarin Forms Layouts

Exception handling

SQL Database and API in Xamarin Forms.

Create your own api with Microsoft SQL database and implemente them in Xamarin forms application.

Xamarin Relative Layout

The usage of ForceLayout in this case

Label's and button's size change according to the text inside of them. Therefore when the children are added to layout, their size remains 0 in both width and height. For example:

relativeLayout.Children.Add(label,
    Constraint.RelativeToParent(parent => label.Width));

Above expression will return 0 because width is 0 at the moment. In order to work around this, we need to listen for SizeChanged event and when the size changes we should force the layout in order to redraw it.

label.SizeChanged += (s, e) => relativeLayout.ForceLayout();

For a view like BoxView this is unnecessary. Because we can define their sizes on instantiation. The other things is, in both cases we can define their width and height as a constraint when we are adding them to the layout. For example:

relativeLayout.Children.Add(label,
    Constraint.Constant(0),
    Constraint.Constant(0),
    //Width constraint
    Constraint.Constant(30),
    //Height constraint
    Constraint.Constant(40));

This will add the label to the point 0, 0. The label's width and height will be 30 and 40. However, if the text is too long, some of it might not show. If your label has or might have high height, you can use LineBreakMode property of label. Which can wrap the text. There are a lot of options in LineBreakMode enum.

Creating custom controls

Platform-specific behaviour

Target Platforms

if(Device.OS == TargetPlatform.Android)
{

}
else if (Device.OS == TargetPlatform.iOS)
{

}
else if (Device.OS == TargetPlatform.WinPhone)
{

}
else if (Device.OS == TargetPlatform.Windows)
{

}
else if (Device.OS == TargetPlatform.Other)
{

}

Contact Picker - Xamarin Forms (Android and iOS)

Contact Picker XF (Android and iOS)

Why Xamarin Forms and When to use Xamarin Forms

Xamarin Plugin

Xamarin.Forms Page

Xamarin.Forms Views

Xamarin.Forms Cells

Xamarin Gesture

Xamarin Gesture

Generic Xamarin.Forms app lifecycle? Platform-dependant!

OAuth2

Effects

Using ListViews

MessagingCenter