Xamarin.Forms Shared code


Example

After you've created and registered your platform-specific classes you can start hooking them up to your shared code. The following page contains a button that triggers the text-to-speech functionality using a pre-defined sentence. It uses DependencyService to retrieve a platform-specific implementation of ITextToSpeech at run time using the native SDKs.

public MainPage ()
{
    var speakButton = new Button {
        Text = "Talk to me baby!",
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand,
    };
    
    speakButton.Clicked += (sender, e) => {
        DependencyService.Get<ITextToSpeech>().Speak("Xamarin Forms likes eating cake by the ocean.");
    };
    
    Content = speakButton;
}

When you run this application on an iOS or Android device and tap the button you will hear the application speak the given sentence.