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.
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.
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.
When using DependencyService
you typically need 3 parts:
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.
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.
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.
Picker.Items
Property Is Not BindableThis 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>
If you're going to run your project on another computer, you'll need to generate a new API key for it, because SHA-1 fingerprints will not match for different build machines.
You can explore the project, described in example Adding a map in Xamarin.Forms here
There is no uniform way to handle push notifications in Xamarin Forms since the implementation relies heavily on platform specific features and events. Therefor platform specific code will always be necessary.
However, by using the DependencyService
you can share as much code as possible. Also there is a plugin designed for this by rdelrosario, which can be found on his GitHub.
Code and screenshots are taken from a blog series by Gerald Versluis which explains the process in more detail.
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
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.
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).
Access platform specific API from PCL or Shared project.
Create your own api with Microsoft SQL database and implemente them in Xamarin forms application.
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.
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 XF (Android and iOS)
You can refer to the official Xamarin Forms documentation to explore more: