Posted on Leave a comment

Override hardware back button Xamarin.Forms

Some phones support the hardware back button and this may disturb the designed navigation experience that has been implemented in the application. You can override this behavior on Android (iOS does not support a hardware back button) with the following code that has to be placed in the MainActivity.cs of your Android root folder after onCreate method.

 public override void OnBackPressed()

The result should look like below. After this action the hardware back button will have no effect and the whole navigation can be performed through the application.

Posted on Leave a comment

Send signals between Mainpage.xaml.cs and Viewmodel – Xamarin.Forms

In a recent application I wanted to inform a Listview with a specific ItemTemplate to update its data source on back button with a synchronous data fetching mechanism(so this was not automatic).

So the goal was to send a “signal” to my Viewmodel when the page was loaded to perform an action. This can be done using MessageCenter with Xamarin.Forms.

The solution was to send an update signal every time OnAppearing() method of the MainPage.xaml was hit and inform the ViewModel to perfom an action.

 protected override void OnAppearing()
            MessagingCenter.Send<nameofapp.App>((nameofapp.App)Xamarin.Forms.Application.Current, "update");

Then perform action on MainPageViewModel

public MainPageViewModel()
            MessagingCenter.Subscribe<nameofapp.App>((nameofapp.App)Application.Current, "update", (sender) => {
                var result = viewmodel.fetchData();




Posted on 4 Comments

Bottom Navigation Bar with Control – Xamarin.Forms

Ever wondered how to create a Bottom Navigation Bar for Xamarin.Forms as it is in the latest Android sdk version? As there is no native control for Xamarin.Forms you could create a custom one with a Control.

First create a Folder and name it Controls and create the control in there.

In your portable class library -> Add New Item -> Content View and name it NavigationBar or something else.


For my purposes I used the following code:

 <Grid   BackgroundColor="Blue" >

                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />

            <StackLayout Orientation="Vertical" Grid.Column="0" HorizontalOptions="CenterAndExpand" VerticalOptions="End">
                <Image  Source="Home.png"  WidthRequest="30" HeightRequest="30" />
                <Label Text="Home" TextColor="White"  />

            <StackLayout Orientation="Vertical"  Grid.Column="1"  HorizontalOptions="CenterAndExpand" VerticalOptions="End">
                <Image Source="Pages.png" WidthRequest="30" HeightRequest="30"  />
                <Label Text="Pages" TextColor="White"  />

            <StackLayout Orientation="Vertical"  Grid.Column="2"  HorizontalOptions="CenterAndExpand" VerticalOptions="End">
                <Image  Source="Settings.png"  WidthRequest="30" HeightRequest="30" />
                <Label Text="Settings" TextColor="White" />

In order to use it in your code reference it in your xaml


And then just use

<controls:NavigationBar />

Finally you can use your control and it will look the same in all platforms.

Posted on Leave a comment

Local Database with Realm – Xamarin.Forms

Realm is a very popular solution for mobile databases as it includes very nice features, it’s very quick and easy to use. It supports many platforms as well as Xamarin.

You can use realm with Xamarin.Forms after installing the nuget package. Be careful to install nuget package to Android, iOS and UWP seperate project.

Current realm version is 1.5. After choosing Realm package, nuget will also install all available requirements.

First of all in order to store items in the database, you should create a model which will represent the identity that you want to save. For example lets say that we want a Person identity so we will store Persons.

Lets create the necessary model for this case:

public class Person
public string Name { get; set; }
public int Age { get; set; }

After that we  will create a test interface in which we can add some users and interact with realm.

  <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
        <Entry  x:Name="name" Placeholder="Enter Name" Margin="0,20,0,0" WidthRequest="200"  />
        <Entry x:Name="age" Placeholder="Enter Age" Margin="0,20,0,0"  WidthRequest="200" />
        <Button Text="Add a new Item" Margin="0,20,0,0" Clicked="addperson" />
        <Button Text="Load all items" Margin="0,20,0,0" Clicked="loadpersons" />


To interact with the database lets create a RealHelper class which we will use to add persons and load.

public class RealmHelper
        Realm realm;
        Transaction transaction;

        public RealmHelper()
            realm = Realm.GetInstance();

        public void AddItem(Person person)

            transaction = realm.BeginWrite();
            var entry = realm.Add(person);

        public IEnumerable<Person> GetItems()
            return realm.All<Person>();


In order to make a write operation we use a transaction and we do that by creating a write with beginWrite(). More information for the functions and how they operation can be found on the documentation.



 RealmHelper rh;
public MainPage()
            rh = new RealmHelper();


The main logic comes from the two buttons:

private async void addperson(object sender, EventArgs e)
            var person = new Person();
            person.Name = name.Text;
            person.Age = age.Text;
            await DisplayAlert("Ok", "A new person was added", "OK");

        private void loadpersons(object sender, EventArgs e)
            IEnumerable<Person> collection = rh.GetItems();



Putting a breakpoint in the loadPersons button we can see the stored persons.


initialization issues I found:

  • In the UWP project you must install realm database nuget package and not realm nuget package. That’s because some functions don’t exist in the uwp apis so if you install realm package some exceptions will occur during the opening of the app.
  • Check if fody file exists in both iOS, Android and UWP projects and the its content should be
<?xml version="1.0" encoding="utf-8"?>
  • Be sure that all packages are updated across all projects and are the same version.