| Start |
|---|
| Implementing text-to-speech A good example of a feature that request platform specific code is when you want to implement text-to-speech (tts). This example assumes that you are working with shared code in a PCL library. A schematic overview of our solution would look like the image underneath. In our shared code we define an interface which is registered with the Now in each specific platform, we need to create an implementation of this interface. Let's start with the iOS implementation. iOS ImplementationIn the code example above you notice that there is specific code to iOS. Like types such as To register this implementation with the Xamarin Now when you do a call like this in your shared code, the right implementation for the platform you are running your app on is injected.
Android ImplementationThe Android implementation of this code would look like underneath. Again don't forget to register it with the Windows Phone ImplementationFinally, for Windows Phone this code can be used. And once more do not forget to register it. Implementing in Shared CodeNow everything is in place to make it work! Finally, in your shared code you can now call this function by using the interface. At runtime, the implementation will be injected which corresponds to the current platform it is running on. In this code you will see a page that could be in a Xamarin Forms project. It creates a button which invokes the The result will be that when the app is ran and the button is clicked, the text provided will be spoken. All of this without having to do hard stuff like compiler hints and such. You now have one uniform way of accessing platform specific functionality through platform independent code. |
| Getting Application and Device OS Version Numbers - Android & iOS - PCL The example below will collect the Device's OS version number and the the version of the application (which is defined in each projects' properties) that is entered into Version name on Android and Version on iOS. First make an interface in your PCL project: Now we implement the interface in the Android and iOS projects. Android: iOS: Now to use the code in a method: RemarksIf you do not want your code to break when no implementation is found, check the You can do this by a simple check if it is not or, if your IDE supports C# 6, with null-conditional operator: If you don't do this and no implementation is found at runtime, your code will generate an exception. |
Read Article
https://develop.horje.com/learn/1434/reference