New to Telerik UI for Xamarin? Download free 30-day trial

Text Analysis


This article will guide you through the process of integrating Azure's Text Analytics API with the controls from the Telerik UI for Xamarin suite. More specifically, we are going to use the RadButton, RadEntry and RadHorizontalLinearGauge controls in a page which can be used to determine whether the text which is entered is in English or not as well as analyze its sentiment. The more positive the input message is - the closer the result will be to 100 on the Gauge scale.

Create a Text Analytics API Account

Before you start, you need to create a new Text Analytics API account through the Azure portal. This has been explained in great detail in this article.

Azure Resource Selection

Once you've created the account, you have to obtain the subscription keys that have been generated for you. You will require them later on in order to make API calls from your application.

Create the Application

Once you have activated the Text Analytics service, you can proceed with creating a new Xamarin.Forms application. We are going to use the following page for the purposes of the example:

<ContentPage xmlns=""
    <StackLayout Padding="20" Margin="0,40,0,0">
        <telerikInput:RadEntry x:Name="entry" 
                       WatermarkText="Text to analyze..."/>
        <telerikInput:RadButton Text="Analyze"
        <telerikGauges:RadHorizontalGauge x:Name="gauge">
                <telerikGauges:GaugeLinearAxis Maximum="100" 
                <telerikGauges:GaugeShapeIndicator Value="{Binding Confidence}" />
                    <telerikGauges:GaugeRange Color="Green" 
                                  To="80" />
                    <telerikGauges:GaugeGradientRange From="80" 
                        <telerikCommon:RadGradientStop Offset="80" 
                                           Color="Yellow" />
                        <telerikCommon:RadGradientStop Offset="100" 
                                           Color="Red" />

Add the Microsoft.Azure.CognitiveServices.Language NuGet Package

You can now add the Microsoft.Azure.CognitiveServices.Language.TextAnalytics NuGet package which provides access to the Microsoft Cognitive Services Language APIs.

Add the Microsoft.Azure.CognitiveServices.Language NuGet Package

At the time of writing this article, the package is in preview, be sure to check "Include prerelease" to get a search result.

Using the Text Analytics API

The following code snippet shows the code-behind of the previously created page where the Text Analytics API is used:

public partial class MainPage : ContentPage, INotifyPropertyChanged

    const string subscriptionKey = "your subscription key";
    private ITextAnalyticsAPI client;
    public MainPage()
        this.BindingContext = this;

        client = new TextAnalyticsAPI();
        client.AzureRegion = AzureRegions.Eastus2;
        client.SubscriptionKey = subscriptionKey;

        btn.Clicked += Btn_ClickedAsync;

    private double confidence;

    public double Confidence
        get { return confidence; }
            this.confidence = value;

    private async Task<double?> MakeAnalysisRequest(string text)
        LanguageBatchResult languageResult = await client.DetectLanguageAsync(
                new BatchInput(
                    new List<Input>()
                        new Input("1", text)

        var language = languageResult.Documents.First().DetectedLanguages.First();

        SentimentBatchResult sentimentResult = await client.SentimentAsync(
            new MultiLanguageBatchInput(new List<MultiLanguageInput>()
                new MultiLanguageInput(language.Iso6391Name, "1", text)

        var sentiment = sentimentResult.Documents.First();

        return (language.Score + sentiment.Score) / 2;

    private async void Btn_ClickedAsync(object sender, EventArgs e)
        var result = await MakeAnalysisRequest(entry.Text);
        this.Confidence = (double)(result * 100);

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

Make sure you use the correct region for client.AzureRegion. You'll get an unauthorized exception returned from the API if your service is not located in the defined region. You can find where your service is located in the Overview blade:

Azure Region

Running the application and typing something positive in English should give you a confidence level close to 100. Here is the actual result:

UWP Result

See Also

In this article
Not finding the help you need? Improve this article