This tutorial shows you the base of any structure of the Xamarin project with a short intro into the Xamarin.Essentials install area.
Take a look at the next image:
First, if the new default project doesn’t have the Xamarin.Essential ( see point 4 on image) then you need to install it with the NuGet Packages tool.
You can use from main menu Tool or right-click on the solution name and select the Manage NuGet Packages for Solution… to add it.
I used the last stable version 1.5.3.2 of Xamarin.Essentials.
This project solution comes with a default tree with items that let you build an Android application.
The first item (see point 1 on image) is that part of the project for the android area.
The android interface is created with a XAML file and is named MainPage.xaml (see point 2 on image), see source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?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:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Xamarin_Essentials_001.MainPage"> <StackLayout> <!-- Place new controls here --> <Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /> </StackLayout> </ContentPage> |
The last part is the MainActivity.cs file (see point 3 on image) and that is used to create the base Android application, see 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 | using System; using Android.App; using Android.Content.PM; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; namespace Xamarin_Essentials_001.Droid { [Activity(Label = "Xamarin_Essentials_001", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); } 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); } } } |
These methods are based on the android activity lifecycle from the documentation webpage.
The Resources folder has images and files and some android permissions and indents are into the AndroidManifest.xml file and MainPage.xaml.cs file.
The next step is to create a C# class named MyStaticClass into the default project Xamarin_Essentials_001 and link it
In Xamarin.Forms, all views have the property named BindingContext
this specifies the object that a view should data-bind with.
When coding with XAML, we can use the x:Static markup extension to reference a static C# property and explicitly provide an instance of a C# class as the binding context:
1 | <Entry BindingContext="{x:Static local:MyStaticClass.MyStaticProperty}"/> |
About the binding process you can read more information on the official webpage.