Today’s tutorial is about C# and the google firebase service.
The main goal is to connect a Xamarin Application with an Firebase Database.
You need to create a simple database without any records on this.
Open your Firebase account and create a database , you will see the URL of this database like this:
1 | https://xamarinapp-YOUR_DATABASE.firebasedatabase.app/ |
To use this service I used an NuGet package called firebase database from this project.
Open a new Xamarin project and install this package.
In the main file named MainActivity.cs I add this source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | using Android.App; using Android.OS; using Android.Runtime; using Android.Widget; using AndroidX.AppCompat.App; using Firebase.Database; namespace XamarinApp { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : AppCompatActivity { Button button; TextView mytextView; private readonly FirebaseClient firebaseClient; protected override void OnCreate(Bundle savedInstanceState) { FirebaseClient firebaseClient = new Firebase.Database.FirebaseClient( "https://xamarinapp-YOUR_DATABASE.firebasedatabase.app/"); base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); button = FindViewById<Button>(Resource.Id.button1); this.FindViewById<Button>(Resource.Id.button1).Click += this.Button_Clicked; } void Button_Clicked(System.Object sender, System.EventArgs e) { mytextView = FindViewById<TextView>(Resource.Id.textView1); this.FindViewById<TextView>(Resource.Id.textView1).Text = "Is set!"; } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } } } |
The UI from activity_main.xml is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="2" android:columnCount="2"> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="25px" android:minHeight="25px" android:id="@+id/button1" android:onClick="Button_Clicked"/> <TextView android:text="Not connect !" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" /> </GridLayout> </RelativeLayout> |
If you run it the when you press the button the text show you one message about the connection with the firebase database.