Components Context can store global level variables, like for example service container references. In the following sample we'll see how to get access to services inside a component.
Create a new Xamarin Forms project using the Hellp World tutorial (called for example RXAppContainer), add a new .NET standard project (for example called RxAppContainer.Services) and add a reference to it from the XF project, finally remove the MainPage.xaml.
Create a simple service with a method that add 2 numbers in the RxAppContainer.Services project:
publicinterfaceICalcService{doubleAdd(double x,double y);}publicclassCalcService:ICalcService{publicdoubleAdd(double x,double y) => x + y;}
Than create a component in the XF project like the following:
In this sample I'm going to use the Microsoft Dependency Injection package to collect and provide services to the app.
Add the Microsoft.Extensions.DependencyInjection package to the XF project with the following command or thru Visual Studio tool window: