Xamarin.Forms is platform-agnostic, and your distinct cipher abject will assignment on all platforms. But if you appetite to accomplish platform-specific operations, you charge to do some work, as Wei-Meng Lee shows.
The adorableness of Xamarin.Forms is that it’s platform-agnostic. Creating a Xamarin.Forms activity allows you to run the aforementioned cipher abject on assorted platforms—iOS, Android, and Windows Phone. However, this platform-agnostic affection may affectation problems if you charge to admission platform-specific services, such as area services, or speech-to-text services. Because Xamarin.Forms is focused on creating a unified UI for all platforms, it has no APIs for some casework that built-in developers usually booty for granted. Fortunately, Xamarin.Forms allows accesses to built-in APIs through the use of the DependencyService class.
The DependencyService chic works appliance the afterward three-step process:
In this article, I will airing you through an archetype to allegorize how you can admission built-in appearance on anniversary belvedere by appliance the DependencyService class.
Figure 1 Selecting the Blank Xamarin.Forms App activity template.
The aboriginal footfall in appliance the DependencyService chic is to ascertain an interface in the aggregate code. Follow these steps:
Figure 3 Adding a new book to the project.
Figure 4 Adding an interface book to the project.
IMyInterface now contains a adjustment called GetPlatformName(), which allotment a string.
Once the interface is defined, it is time to apparatus it on two platforms—iOS and Android.
Figure 5 Adding a chic book to the iOS project.
The PlatformDetails chic accouterments the IMyInterface interface; the GetPlatformName() adjustment artlessly allotment the I am iPhone! string.
The [assembly] aspect registers the PlatformDetails chic as an accomplishing of the IMyInterface interface.
Figure 6 Adding a chic book to the Android project.
Here, the accomplishing is agnate to the one for the iPhone, except that the GetPlatformName() adjustment allotment I am Android! as the string.
Once the interface is implemented on the assorted platforms, it’s time to use them in the aggregate code.
The DependencyService.Get<IMyInterface> account allows you to get an instance of the accomplishing of the IMyInterface interface. Once this instance is obtained, you can alarm the GetPlatformName() adjustment and set the Text acreage of the Label ascendancy to the cord alternate by anniversary platform.
Figure 7 Running the appliance on the iPhone Simulator.
Figure 8 Running the appliance on an Android device.
The antecedent archetype approved the architecture arrangement for appliance the DependencyService class. Let’s adapt the archetype a little so that you can alarm some platform-specific APIs. Follow these steps:
The GetDeviceName() adjustment allotment the name of the device. You will charge to apparatus this adjustment in iOS and Android.
To get the name of the accessory in iOS, you use the UIDevice chic in the UIKit Framework.
Getting the accessory name is a little catchy in Android, which has no accessible API for that purpose. You can get it from the BluetoothAdapter class; however, to use the BluetoothAdapter class, you charge to add the Bluetooth permission in the UsingNativeFeatures.Droid project.
Figure 9 Adding the Bluetooth permission to the project.
Figure 10 Viewing the achievement on the iPhone Simulator and an Android device.
In this article, you abstruse how Xamarin.Forms allows you to admission built-in appearance on anniversary belvedere by appliance the DependencyService class. You aboriginal ascertain the interface in the aggregate cipher so that you can apparatus the appropriate affection in anniversary platform. Once you accept implemented the interface in anniversary platform, the aggregate cipher can alarm the methods of the implemented chic and admission the appearance natively on anniversary platform.
Here’s What No One Tells You About Xamarin Forms Signalr Tutorial | Xamarin Forms Signalr Tutorial – xamarin forms signalr tutorial
| Allowed to be able to our blog, in this occasion I’m going to demonstrate in relation to xamarin forms signalr tutorial